pxa_machdep.c revision 330897
1/*	$NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1994-1998 Mark Brinicombe.
7 * Copyright (c) 1994 Brini.
8 * All rights reserved.
9 *
10 * This code is derived from software written for Brini by Mark Brinicombe
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *      This product includes software developed by Brini.
23 * 4. The name of the company nor the name of the author may be used to
24 *    endorse or promote products derived from this software without specific
25 *    prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * RiscBSD kernel project
40 *
41 * machdep.c
42 *
43 * Machine dependent functions for kernel setup
44 *
45 * This file needs a lot of work.
46 *
47 * Created      : 17/09/94
48 */
49
50#include "opt_ddb.h"
51#include "opt_kstack_pages.h"
52
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: stable/11/sys/arm/xscale/pxa/pxa_machdep.c 330897 2018-03-14 03:19:51Z eadler $");
55
56#define _ARM32_BUS_DMA_PRIVATE
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/sysproto.h>
60#include <sys/signalvar.h>
61#include <sys/imgact.h>
62#include <sys/kernel.h>
63#include <sys/ktr.h>
64#include <sys/linker.h>
65#include <sys/lock.h>
66#include <sys/malloc.h>
67#include <sys/mutex.h>
68#include <sys/pcpu.h>
69#include <sys/proc.h>
70#include <sys/ptrace.h>
71#include <sys/cons.h>
72#include <sys/bio.h>
73#include <sys/bus.h>
74#include <sys/buf.h>
75#include <sys/exec.h>
76#include <sys/kdb.h>
77#include <sys/msgbuf.h>
78#include <sys/devmap.h>
79#include <machine/reg.h>
80#include <machine/cpu.h>
81
82#include <vm/vm.h>
83#include <vm/pmap.h>
84#include <vm/vm_object.h>
85#include <vm/vm_page.h>
86#include <vm/vm_map.h>
87#include <machine/vmparam.h>
88#include <machine/pcb.h>
89#include <machine/undefined.h>
90#include <machine/machdep.h>
91#include <machine/metadata.h>
92#include <machine/armreg.h>
93#include <machine/bus.h>
94#include <machine/physmem.h>
95#include <sys/reboot.h>
96
97#include <arm/xscale/pxa/pxareg.h>
98#include <arm/xscale/pxa/pxavar.h>
99
100#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
101#define	KERNEL_PT_IOPXS		1
102#define KERNEL_PT_BEFOREKERN	2
103#define KERNEL_PT_AFKERNEL	3	/* L2 table for mapping after kernel */
104#define	KERNEL_PT_AFKERNEL_NUM	9
105
106/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
107#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
108
109struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
110
111/* Physical and virtual addresses for some global pages */
112
113struct pv_addr systempage;
114struct pv_addr msgbufpv;
115struct pv_addr irqstack;
116struct pv_addr undstack;
117struct pv_addr abtstack;
118struct pv_addr kernelstack;
119struct pv_addr minidataclean;
120
121static void	pxa_probe_sdram(bus_space_tag_t, bus_space_handle_t,
122		    uint32_t *, uint32_t *);
123
124/* Static device mappings. */
125static const struct devmap_entry pxa_devmap[] = {
126	/*
127	 * Map the on-board devices up into the KVA region so we don't muck
128	 * up user-space.
129	 */
130	{
131		PXA2X0_PERIPH_START + PXA2X0_PERIPH_OFFSET,
132		PXA2X0_PERIPH_START,
133		PXA250_PERIPH_END - PXA2X0_PERIPH_START,
134	},
135	{ 0, 0, 0, }
136};
137
138#define SDRAM_START 0xa0000000
139
140extern vm_offset_t xscale_cache_clean_addr;
141
142void *
143initarm(struct arm_boot_params *abp)
144{
145	struct pv_addr  kernel_l1pt;
146	struct pv_addr  dpcpu;
147	int loop;
148	u_int l1pagetable;
149	vm_offset_t freemempos;
150	vm_offset_t freemem_pt;
151	vm_offset_t afterkern;
152	vm_offset_t freemem_after;
153	vm_offset_t lastaddr;
154	int i, j;
155	uint32_t memsize[PXA2X0_SDRAM_BANKS], memstart[PXA2X0_SDRAM_BANKS];
156
157	lastaddr = parse_boot_param(abp);
158	arm_physmem_kernaddr = abp->abp_physaddr;
159	set_cpufuncs();
160	pcpu_init(pcpup, 0, sizeof(struct pcpu));
161	PCPU_SET(curthread, &thread0);
162
163	/* Do basic tuning, hz etc */
164	init_param1();
165
166	freemempos = 0xa0200000;
167	/* Define a macro to simplify memory allocation */
168#define	valloc_pages(var, np)			\
169	alloc_pages((var).pv_pa, (np));		\
170	(var).pv_va = (var).pv_pa + 0x20000000;
171
172#define alloc_pages(var, np)			\
173	freemempos -= (np * PAGE_SIZE);		\
174	(var) = freemempos;		\
175	memset((char *)(var), 0, ((np) * PAGE_SIZE));
176
177	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
178		freemempos -= PAGE_SIZE;
179	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
180	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
181		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
182			valloc_pages(kernel_pt_table[loop],
183			    L2_TABLE_SIZE / PAGE_SIZE);
184		} else {
185			kernel_pt_table[loop].pv_pa = freemempos +
186			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
187			    L2_TABLE_SIZE_REAL;
188			kernel_pt_table[loop].pv_va =
189			    kernel_pt_table[loop].pv_pa + 0x20000000;
190		}
191	}
192	freemem_pt = freemempos;
193	freemempos = 0xa0100000;
194	/*
195	 * Allocate a page for the system page mapped to V0x00000000
196	 * This page will just contain the system vectors and can be
197	 * shared by all processes.
198	 */
199	valloc_pages(systempage, 1);
200
201	/* Allocate dynamic per-cpu area. */
202	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
203	dpcpu_init((void *)dpcpu.pv_va, 0);
204
205	/* Allocate stacks for all modes */
206	valloc_pages(irqstack, IRQ_STACK_SIZE);
207	valloc_pages(abtstack, ABT_STACK_SIZE);
208	valloc_pages(undstack, UND_STACK_SIZE);
209	valloc_pages(kernelstack, kstack_pages);
210	alloc_pages(minidataclean.pv_pa, 1);
211	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
212	/*
213	 * Allocate memory for the l1 and l2 page tables. The scheme to avoid
214	 * wasting memory by allocating the l1pt on the first 16k memory was
215	 * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
216	 * this to work (which is supposed to be the case).
217	 */
218
219	/*
220	 * Now we start construction of the L1 page table
221	 * We start by mapping the L2 page tables into the L1.
222	 * This means that we can replace L1 mappings later on if necessary
223	 */
224	l1pagetable = kernel_l1pt.pv_va;
225
226	/* Map the L2 pages tables in the L1 page table */
227	pmap_link_l2pt(l1pagetable, rounddown2(ARM_VECTORS_HIGH, 0x00100000),
228		       &kernel_pt_table[KERNEL_PT_SYS]);
229#if 0 /* XXXBJR: What is this?  Don't know if there's an analogue. */
230	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
231	                &kernel_pt_table[KERNEL_PT_IOPXS]);
232#endif
233	pmap_link_l2pt(l1pagetable, KERNBASE,
234	    &kernel_pt_table[KERNEL_PT_BEFOREKERN]);
235	pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
236	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
237	pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
238	    0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
239	pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
240	   rounddown2(((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE, L1_S_SIZE),
241	   VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
242	freemem_after = rounddown2((int)lastaddr + PAGE_SIZE, PAGE_SIZE);
243	afterkern = round_page(rounddown2((vm_offset_t)lastaddr + L1_S_SIZE, L1_S_SIZE));
244	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
245		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
246		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
247	}
248	pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
249	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
250
251
252	/* Map the Mini-Data cache clean area. */
253	xscale_setup_minidata(l1pagetable, afterkern,
254	    minidataclean.pv_pa);
255
256	/* Map the vector page. */
257	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
258	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
259	devmap_bootstrap(l1pagetable, pxa_devmap);
260
261	/*
262	 * Give the XScale global cache clean code an appropriately
263	 * sized chunk of unmapped VA space starting at 0xff000000
264	 * (our device mappings end before this address).
265	 */
266	xscale_cache_clean_addr = 0xff000000U;
267
268	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
269	cpu_setttb(kernel_l1pt.pv_pa);
270	cpu_tlb_flushID();
271	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
272
273	/*
274	 * Pages were allocated during the secondary bootstrap for the
275	 * stacks for different CPU modes.
276	 * We must now set the r13 registers in the different CPU modes to
277	 * point to these stacks.
278	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
279	 * of the stack memory.
280	 */
281	set_stackptrs(0);
282
283	/*
284	 * We must now clean the cache again....
285	 * Cleaning may be done by reading new data to displace any
286	 * dirty data in the cache. This will have happened in cpu_setttb()
287	 * but since we are boot strapping the addresses used for the read
288	 * may have just been remapped and thus the cache could be out
289	 * of sync. A re-clean after the switch will cure this.
290	 * After booting there are no gross relocations of the kernel thus
291	 * this problem will not occur after initarm().
292	 */
293	cpu_idcache_wbinv_all();
294	cpu_setup();
295
296	/*
297	 * Sort out bus_space for on-board devices.
298	 */
299	pxa_obio_tag_init();
300
301	/*
302	 * Fetch the SDRAM start/size from the PXA2X0 SDRAM configration
303	 * registers.
304	 */
305	pxa_probe_sdram(obio_tag, PXA2X0_MEMCTL_BASE, memstart, memsize);
306
307	/* Fire up consoles. */
308	cninit();
309
310	undefined_init();
311
312	init_proc0(kernelstack.pv_va);
313
314	/* Enable MMU, I-cache, D-cache, write buffer. */
315	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
316
317	pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
318	vm_max_kernel_address = 0xe0000000;
319	pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
320	msgbufp = (void*)msgbufpv.pv_va;
321	msgbufinit(msgbufp, msgbufsize);
322	mutex_init();
323
324	/*
325	 * Add the physical ram we have available.
326	 *
327	 * Exclude the kernel (and all the things we allocated which immediately
328	 * follow the kernel) from the VM allocation pool but not from crash
329	 * dumps.  virtual_avail is a global variable which tracks the kva we've
330	 * "allocated" while setting up pmaps.
331	 *
332	 * Prepare the list of physical memory available to the vm subsystem.
333	 */
334	for (j = 0; j < PXA2X0_SDRAM_BANKS; j++) {
335		if (memsize[j] > 0)
336			arm_physmem_hardware_region(memstart[j], memsize[j]);
337	}
338	arm_physmem_exclude_region(freemem_pt, abp->abp_physaddr -
339	    freemem_pt, EXFLAG_NOALLOC);
340	arm_physmem_exclude_region(freemempos, abp->abp_physaddr - 0x100000 -
341	    freemempos, EXFLAG_NOALLOC);
342	arm_physmem_exclude_region(abp->abp_physaddr,
343	    virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC);
344	arm_physmem_init_kernel_globals();
345
346	init_param2(physmem);
347	kdb_init();
348	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
349	    sizeof(struct pcb)));
350}
351
352static void
353pxa_probe_sdram(bus_space_tag_t bst, bus_space_handle_t bsh,
354    uint32_t *memstart, uint32_t *memsize)
355{
356	uint32_t	mdcnfg, dwid, dcac, drac, dnb;
357	int		i;
358
359	mdcnfg = bus_space_read_4(bst, bsh, MEMCTL_MDCNFG);
360
361	/*
362	 * Scan all 4 SDRAM banks
363	 */
364	for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
365		memstart[i] = 0;
366		memsize[i] = 0;
367
368		switch (i) {
369		case 0:
370		case 1:
371			if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
372			    (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
373				continue;
374			dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
375			dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
376			drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
377			dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
378			break;
379
380		case 2:
381		case 3:
382			if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
383			    (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
384				continue;
385			dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
386			dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
387			drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
388			dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
389			break;
390		default:
391			panic("pxa_probe_sdram: impossible");
392		}
393
394		dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK));  /* 16/32 width */
395		dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8);  /* 8-11 columns */
396		drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
397		dnb = 2 << (dnb & MDCNFD_DNB_MASK);	      /* # of banks */
398
399		memsize[i] = dwid * dcac * drac * dnb;
400		memstart[i] = PXA2X0_SDRAM0_START +
401		    (i * PXA2X0_SDRAM_BANK_SIZE);
402	}
403}
404
405#define	TIMER_FREQUENCY	3686400
406#define	UNIMPLEMENTED	panic("%s: unimplemented", __func__)
407
408/* XXXBJR: Belongs with DELAY in a timer.c of some sort. */
409void
410cpu_startprofclock(void)
411{
412	UNIMPLEMENTED;
413}
414
415void
416cpu_stopprofclock(void)
417{
418	UNIMPLEMENTED;
419}
420
421static struct arm32_dma_range pxa_range = {
422	.dr_sysbase = 0,
423	.dr_busbase = 0,
424	.dr_len = ~0u,
425};
426
427struct arm32_dma_range *
428bus_dma_get_range(void)
429{
430
431	return (&pxa_range);
432}
433
434int
435bus_dma_get_range_nb(void)
436{
437
438	return (1);
439}
440