xref: /titanic_51/usr/src/uts/sun4u/daktari/os/daktari.c (revision 0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/cpuvar.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sunddi.h>
32 #include <sys/ddi.h>
33 #include <sys/esunddi.h>
34 #include <sys/sysmacros.h>
35 #include <sys/note.h>
36 
37 #include <sys/modctl.h>		/* for modload() */
38 #include <sys/platform_module.h>
39 #include <sys/errno.h>
40 #include <sys/daktari.h>
41 #include <sys/machsystm.h>
42 #include <sys/promif.h>
43 #include <vm/page.h>
44 #include <sys/memnode.h>
45 #include <vm/vm_dep.h>
46 
47 /* I2C Stuff */
48 #include <sys/i2c/clients/i2c_client.h>
49 
50 
51 int (*p2get_mem_unum)(int, uint64_t, char *, int, int *);
52 
53 /* Daktari Keyswitch Information */
54 #define	DAK_KEY_POLL_PORT	3
55 #define	DAK_KEY_POLL_BIT	2
56 #define	DAK_KEY_POLL_INTVL	10
57 
58 static	boolean_t	key_locked_bit;
59 static	clock_t		keypoll_timeout_hz;
60 
61 /*
62  * Table that maps memory slices to a specific memnode.
63  */
64 int slice_to_memnode[DAK_MAX_SLICE];
65 
66 /*
67  * For software memory interleaving support.
68  */
69 static	void update_mem_bounds(int, int, int, uint64_t, uint64_t);
70 
71 static uint64_t
72 slice_table[DAK_SBD_SLOTS][DAK_CPUS_PER_BOARD][DAK_BANKS_PER_MC][2];
73 
74 #define	SLICE_PA	0
75 #define	SLICE_SPAN	1
76 
77 int (*daktari_ssc050_get_port_bit) (dev_info_t *, int, int, uint8_t *, int);
78 extern	void (*abort_seq_handler)();
79 static	int daktari_dev_search(dev_info_t *, void *);
80 static	void keyswitch_poll(void *);
81 static	void daktari_abort_seq_handler(char *msg);
82 
83 void
84 startup_platform(void)
85 {
86 	/*
87 	 * Disable an active h/w watchdog timer
88 	 * upon exit to OBP.
89 	 */
90 	extern int disable_watchdog_on_exit;
91 	disable_watchdog_on_exit = 1;
92 }
93 
94 int
95 set_platform_tsb_spares()
96 {
97 	return (0);
98 }
99 
100 #pragma weak mmu_init_large_pages
101 
102 void
103 set_platform_defaults(void)
104 {
105 	extern void mmu_init_large_pages(size_t);
106 
107 	if ((mmu_page_sizes == max_mmu_page_sizes) &&
108 	    (mmu_ism_pagesize != DEFAULT_ISM_PAGESIZE)) {
109 		if (&mmu_init_large_pages)
110 			mmu_init_large_pages(mmu_ism_pagesize);
111 	}
112 }
113 
114 void
115 load_platform_modules(void)
116 {
117 	if (modload("misc", "pcihp") < 0) {
118 		cmn_err(CE_NOTE, "pcihp driver failed to load");
119 	}
120 	if (modload("drv", "pmc") < 0) {
121 		cmn_err(CE_NOTE, "pmc driver failed to load");
122 	}
123 
124 }
125 
126 void
127 load_platform_drivers(void)
128 {
129 	char **drv;
130 	dev_info_t	*keysw_dip;
131 
132 	static char *boot_time_drivers[] = {
133 		"hpc3130",
134 		"todds1287",
135 		"mc-us3",
136 		"ssc050",
137 		"pcisch",
138 		NULL
139 	};
140 
141 	for (drv = boot_time_drivers; *drv; drv++) {
142 		if (i_ddi_attach_hw_nodes(*drv) != DDI_SUCCESS)
143 			cmn_err(CE_WARN, "Failed to install \"%s\" driver.",
144 			    *drv);
145 	}
146 
147 	/*
148 	 * mc-us3 & ssc050 must stay loaded for plat_get_mem_unum()
149 	 * and keyswitch_poll()
150 	 */
151 	(void) ddi_hold_driver(ddi_name_to_major("mc-us3"));
152 	(void) ddi_hold_driver(ddi_name_to_major("ssc050"));
153 
154 	/* Gain access into the ssc050_get_port function */
155 	daktari_ssc050_get_port_bit = (int (*) (dev_info_t *, int, int,
156 	    uint8_t *, int)) modgetsymvalue("ssc050_get_port_bit", 0);
157 	if (daktari_ssc050_get_port_bit == NULL) {
158 		cmn_err(CE_WARN, "cannot find ssc050_get_port_bit");
159 		return;
160 	}
161 
162 	ddi_walk_devs(ddi_root_node(), daktari_dev_search, (void *)&keysw_dip);
163 	ASSERT(keysw_dip != NULL);
164 
165 	/*
166 	 * prevent detach of i2c-ssc050
167 	 */
168 	e_ddi_hold_devi(keysw_dip);
169 
170 	keypoll_timeout_hz = drv_usectohz(10 * MICROSEC);
171 	keyswitch_poll(keysw_dip);
172 	abort_seq_handler = daktari_abort_seq_handler;
173 }
174 
175 static int
176 daktari_dev_search(dev_info_t *dip, void *arg)
177 {
178 	char		*compatible = NULL; /* Search tree for "i2c-ssc050" */
179 	int		*dev_regs; /* Info about where the device is. */
180 	uint_t		len;
181 	int		err;
182 
183 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
184 	    "compatible", &compatible) != DDI_PROP_SUCCESS)
185 		return (DDI_WALK_CONTINUE);
186 
187 	if (strcmp(compatible, "i2c-ssc050") == 0) {
188 		ddi_prop_free(compatible);
189 
190 		err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
191 		    DDI_PROP_DONTPASS, "reg", &dev_regs, &len);
192 		if (err != DDI_PROP_SUCCESS) {
193 			return (DDI_WALK_CONTINUE);
194 		}
195 		/*
196 		 * regs[0] contains the bus number and regs[1]
197 		 * contains the device address of the i2c device.
198 		 * 0x82 is the device address of the i2c device
199 		 * from which  the key switch position is read.
200 		 */
201 		if (dev_regs[0] == 0 && dev_regs[1] == 0x82) {
202 			*((dev_info_t **)arg) = dip;
203 			ddi_prop_free(dev_regs);
204 			return (DDI_WALK_TERMINATE);
205 		}
206 		ddi_prop_free(dev_regs);
207 	} else {
208 		ddi_prop_free(compatible);
209 	}
210 	return (DDI_WALK_CONTINUE);
211 }
212 
213 static void
214 keyswitch_poll(void *arg)
215 {
216 	dev_info_t	*dip = arg;
217 	uchar_t	port_byte;
218 	int	port = DAK_KEY_POLL_PORT;
219 	int	bit = DAK_KEY_POLL_BIT;
220 	int	err;
221 
222 	err = daktari_ssc050_get_port_bit(dip, port, bit,
223 	    &port_byte, I2C_NOSLEEP);
224 	if (err != 0) {
225 		cmn_err(CE_WARN, "keyswitch polling disabled: "
226 		    "errno=%d while reading ssc050", err);
227 		return;
228 	}
229 
230 	key_locked_bit = (boolean_t)((port_byte & 0x1));
231 	timeout(keyswitch_poll, (caddr_t)dip, keypoll_timeout_hz);
232 }
233 
234 static void
235 daktari_abort_seq_handler(char *msg)
236 {
237 	if (key_locked_bit == 0)
238 		cmn_err(CE_CONT, "KEY in LOCKED position, "
239 		    "ignoring debug enter sequence");
240 	else  {
241 		debug_enter(msg);
242 	}
243 }
244 
245 
246 int
247 plat_cpu_poweron(struct cpu *cp)
248 {
249 	_NOTE(ARGUNUSED(cp))
250 	return (ENOTSUP);
251 }
252 
253 int
254 plat_cpu_poweroff(struct cpu *cp)
255 {
256 	_NOTE(ARGUNUSED(cp))
257 	return (ENOTSUP);
258 }
259 
260 /*
261  * Given a pfn, return the board and beginning/end of the page's
262  * memory controller's address range.
263  */
264 static int
265 plat_discover_slice(pfn_t pfn, pfn_t *first, pfn_t *last)
266 {
267 	int bd, cpu, bank;
268 
269 	for (bd = 0; bd < DAK_SBD_SLOTS; bd++) {
270 		for (cpu = 0; cpu < DAK_CPUS_PER_BOARD; cpu++) {
271 			for (bank = 0; bank < DAK_BANKS_PER_MC; bank++) {
272 				uint64_t *slice = slice_table[bd][cpu][bank];
273 				uint64_t base = btop(slice[SLICE_PA]);
274 				uint64_t len = btop(slice[SLICE_SPAN]);
275 				if (len && pfn >= base && pfn < (base + len)) {
276 					*first = base;
277 					*last = base + len - 1;
278 					return (bd);
279 				}
280 			}
281 		}
282 	}
283 	panic("plat_discover_slice: no slice for pfn 0x%lx\n", pfn);
284 	/* NOTREACHED */
285 }
286 
287 /*ARGSUSED*/
288 void
289 plat_freelist_process(int mnode)
290 {}
291 
292 
293 /*
294  * Called for each board/cpu/PA range detected in plat_fill_mc().
295  */
296 static void
297 update_mem_bounds(int boardid, int cpuid, int bankid,
298 	uint64_t base, uint64_t size)
299 {
300 	uint64_t	end;
301 	int		mnode;
302 
303 	slice_table[boardid][cpuid][bankid][SLICE_PA] = base;
304 	slice_table[boardid][cpuid][bankid][SLICE_SPAN] = size;
305 
306 	end = base + size - 1;
307 
308 	/*
309 	 * First see if this board already has a memnode associated
310 	 * with it.  If not, see if this slice has a memnode.  This
311 	 * covers the cases where a single slice covers multiple
312 	 * boards (cross-board interleaving) and where a single
313 	 * board has multiple slices (1+GB DIMMs).
314 	 */
315 	if ((mnode = plat_lgrphand_to_mem_node(boardid)) == -1) {
316 		if ((mnode = slice_to_memnode[PA_2_SLICE(base)]) == -1)
317 			mnode = mem_node_alloc();
318 
319 		ASSERT(mnode >= 0);
320 		ASSERT(mnode < MAX_MEM_NODES);
321 		plat_assign_lgrphand_to_mem_node(boardid, mnode);
322 	}
323 
324 	base = P2ALIGN(base, (1ul << PA_SLICE_SHIFT));
325 
326 	while (base < end) {
327 		slice_to_memnode[PA_2_SLICE(base)] = mnode;
328 		base += (1ul << PA_SLICE_SHIFT);
329 	}
330 }
331 
332 /*
333  * Dynamically detect memory slices in the system by decoding
334  * the cpu memory decoder registers at boot time.
335  */
336 void
337 plat_fill_mc(pnode_t nodeid)
338 {
339 	uint64_t	mc_addr, saf_addr;
340 	uint64_t	mc_decode[DAK_BANKS_PER_MC];
341 	uint64_t	base, size;
342 	uint64_t	saf_mask;
343 	uint64_t	offset;
344 	uint32_t	regs[4];
345 	int		len;
346 	int		local_mc;
347 	int		portid;
348 	int		boardid;
349 	int		cpuid;
350 	int		i;
351 
352 	if ((prom_getprop(nodeid, "portid", (caddr_t)&portid) < 0) ||
353 	    (portid == -1))
354 		return;
355 
356 	/*
357 	 * Decode the board number from the MC portid.  Assumes
358 	 * portid == safari agentid.
359 	 */
360 	boardid = DAK_GETSLOT(portid);
361 	cpuid = DAK_GETSID(portid);
362 
363 	/*
364 	 * The "reg" property returns 4 32-bit values. The first two are
365 	 * combined to form a 64-bit address.  The second two are for a
366 	 * 64-bit size, but we don't actually need to look at that value.
367 	 */
368 	len = prom_getproplen(nodeid, "reg");
369 	if (len != (sizeof (uint32_t) * 4)) {
370 		prom_printf("Warning: malformed 'reg' property\n");
371 		return;
372 	}
373 	if (prom_getprop(nodeid, "reg", (caddr_t)regs) < 0)
374 		return;
375 	mc_addr = ((uint64_t)regs[0]) << 32;
376 	mc_addr |= (uint64_t)regs[1];
377 
378 	/*
379 	 * Figure out whether the memory controller we are examining
380 	 * belongs to this CPU or a different one.
381 	 */
382 	saf_addr = lddsafaddr(8);
383 	saf_mask = (uint64_t)SAF_MASK;
384 	if ((mc_addr & saf_mask) == saf_addr)
385 		local_mc = 1;
386 	else
387 		local_mc = 0;
388 
389 	for (i = 0; i < DAK_BANKS_PER_MC; i++) {
390 		/*
391 		 * Memory decode masks are at offsets 0x10 - 0x28.
392 		 */
393 		offset = 0x10 + (i << 3);
394 
395 		/*
396 		 * If the memory controller is local to this CPU, we use
397 		 * the special ASI to read the decode registers.
398 		 * Otherwise, we load the values from a magic address in
399 		 * I/O space.
400 		 */
401 		if (local_mc)
402 			mc_decode[i] = lddmcdecode(offset);
403 		else
404 			mc_decode[i] = lddphysio(mc_addr | offset);
405 
406 		/*
407 		 * If the upper bit is set, we have a valid mask
408 		 */
409 		if ((int64_t)mc_decode[i] < 0) {
410 			/*
411 			 * The memory decode register is a bitmask field,
412 			 * so we can decode that into both a base and
413 			 * a span.
414 			 */
415 			base = MC_BASE(mc_decode[i]) << PHYS2UM_SHIFT;
416 			size = MC_UK2SPAN(mc_decode[i]);
417 			update_mem_bounds(boardid, cpuid, i, base, size);
418 		}
419 	}
420 }
421 
422 
423 /*
424  * This routine is run midway through the boot process.  By the time we get
425  * here, we know about all the active CPU boards in the system, and we have
426  * extracted information about each board's memory from the memory
427  * controllers.  We have also figured out which ranges of memory will be
428  * assigned to which memnodes, so we walk the slice table to build the table
429  * of memnodes.
430  */
431 /* ARGSUSED */
432 void
433 plat_build_mem_nodes(prom_memlist_t *list, size_t  nelems)
434 {
435 	int	slice;
436 	pfn_t   basepfn;
437 	pgcnt_t npgs;
438 
439 	mem_node_pfn_shift = PFN_SLICE_SHIFT;
440 	mem_node_physalign = (1ull << PA_SLICE_SHIFT);
441 	npgs = 1ull << PFN_SLICE_SHIFT;
442 
443 	for (slice = 0; slice < DAK_MAX_SLICE; slice++) {
444 		if (slice_to_memnode[slice] == -1)
445 			continue;
446 		basepfn = (uint64_t)slice << PFN_SLICE_SHIFT;
447 		mem_node_add_slice(basepfn, basepfn + npgs - 1);
448 	}
449 }
450 
451 
452 
453 /*
454  * Daktari support for lgroups.
455  *
456  * On Daktari, an lgroup platform handle == slot number.
457  *
458  * Mappings between lgroup handles and memnodes are managed
459  * in addition to mappings between memory slices and memnodes
460  * to support cross-board interleaving as well as multiple
461  * slices per board (e.g. >1GB DIMMs). The initial mapping
462  * of memnodes to lgroup handles is determined at boot time.
463  */
464 int
465 plat_pfn_to_mem_node(pfn_t pfn)
466 {
467 	return (slice_to_memnode[PFN_2_SLICE(pfn)]);
468 }
469 
470 /*
471  * Return the platform handle for the lgroup containing the given CPU
472  *
473  * For Daktari, lgroup platform handle == slot number
474  */
475 lgrp_handle_t
476 plat_lgrp_cpu_to_hand(processorid_t id)
477 {
478 	return (DAK_GETSLOT(id));
479 }
480 
481 /*
482  * Platform specific lgroup initialization
483  */
484 void
485 plat_lgrp_init(void)
486 {
487 	int i;
488 
489 	/*
490 	 * Initialize lookup tables to invalid values so we catch
491 	 * any illegal use of them.
492 	 */
493 	for (i = 0; i < DAK_MAX_SLICE; i++) {
494 		slice_to_memnode[i] = -1;
495 	}
496 }
497 
498 /*
499  * Return latency between "from" and "to" lgroups
500  *
501  * This latency number can only be used for relative comparison
502  * between lgroups on the running system, cannot be used across platforms,
503  * and may not reflect the actual latency.  It is platform and implementation
504  * specific, so platform gets to decide its value.  It would be nice if the
505  * number was at least proportional to make comparisons more meaningful though.
506  * NOTE: The numbers below are supposed to be load latencies for uncached
507  * memory divided by 10.
508  */
509 int
510 plat_lgrp_latency(lgrp_handle_t from, lgrp_handle_t to)
511 {
512 	/*
513 	 * Return min remote latency when there are more than two lgroups
514 	 * (root and child) and getting latency between two different lgroups
515 	 * or root is involved
516 	 */
517 	if (lgrp_optimizations() && (from != to ||
518 	    from == LGRP_DEFAULT_HANDLE || to == LGRP_DEFAULT_HANDLE))
519 		return (21);
520 	else
521 		return (19);
522 }
523 /*
524  * No platform drivers on this platform
525  */
526 char *platform_module_list[] = {
527 	(char *)0
528 };
529 
530 /*ARGSUSED*/
531 void
532 plat_tod_fault(enum tod_fault_type tod_bad)
533 {
534 }
535 
536 /*ARGSUSED*/
537 int
538 plat_get_mem_unum(int synd_code, uint64_t flt_addr, int flt_bus_id,
539     int flt_in_memory, ushort_t flt_status, char *buf, int buflen, int *lenp)
540 {
541 	if (flt_in_memory && (p2get_mem_unum != NULL))
542 		return (p2get_mem_unum(synd_code, P2ALIGN(flt_addr, 8),
543 		    buf, buflen, lenp));
544 	else
545 		return (ENOTSUP);
546 }
547 
548 /*
549  * This platform hook gets called from mc_add_mem_unum_label() in the mc-us3
550  * driver giving each platform the opportunity to add platform
551  * specific label information to the unum for ECC error logging purposes.
552  */
553 void
554 plat_add_mem_unum_label(char *unum, int mcid, int bank, int dimm)
555 {
556 	_NOTE(ARGUNUSED(bank, dimm))
557 
558 	char board = DAK_GETSLOT_LABEL(mcid);
559 	char old_unum[UNUM_NAMLEN];
560 
561 	strcpy(old_unum, unum);
562 	snprintf(unum, UNUM_NAMLEN, "Slot %c: %s", board, old_unum);
563 }
564 
565 int
566 plat_get_cpu_unum(int cpuid, char *buf, int buflen, int *lenp)
567 {
568 	char board = DAK_GETSLOT_LABEL(cpuid);
569 
570 	if (snprintf(buf, buflen, "Slot %c", board) >= buflen) {
571 		return (ENOSPC);
572 	} else {
573 		*lenp = strlen(buf);
574 		return (0);
575 	}
576 }
577 
578 /*
579  * The zuluvm module requires a dmv interrupt for each installed zulu board.
580  */
581 void
582 plat_dmv_params(uint_t *hwint, uint_t *swint)
583 {
584 	*hwint = 0;
585 	*swint = DAK_SBD_SLOTS - 1;
586 }
587