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 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25/*
26 * Copyright 2019 Peter Tribble.
27 */
28
29/*
30 * sun4u root nexus driver
31 */
32#include <sys/conf.h>
33#include <sys/cpuvar.h>
34#include <sys/sysiosbus.h>
35#include <sys/intreg.h>
36#include <sys/ddi_subrdefs.h>
37#include <sys/sunndi.h>
38#include <sys/async.h>
39
40/* Useful debugging Stuff */
41#include <sys/nexusdebug.h>
42#define	ROOTNEX_MAP_DEBUG		0x1
43#define	ROOTNEX_INTR_DEBUG		0x2
44
45/*
46 * Extern declarations
47 */
48extern uint_t	root_phys_addr_lo_mask;
49extern int rootnex_name_child(dev_info_t *child, char *name, int namelen);
50extern int rootnex_ctl_uninitchild(dev_info_t *dip);
51
52uint_t	root_phys_addr_hi_mask = 0xffffffff;
53
54/*
55 * config information
56 */
57int
58rootnex_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
59    ddi_intr_handle_impl_t *hdlp);
60
61int
62rootnex_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
63    ddi_intr_handle_impl_t *hdlp);
64
65int
66rootnex_get_intr_pri(dev_info_t *dip, dev_info_t *rdip,
67    ddi_intr_handle_impl_t *hdlp);
68
69ddi_iblock_cookie_t rootnex_err_ibc;
70
71/*
72 * rootnex_add_intr_impl:
73 */
74/*ARGSUSED*/
75int
76rootnex_add_intr_impl(dev_info_t *dip, dev_info_t *rdip,
77    ddi_intr_handle_impl_t *hdlp)
78{
79	volatile uint64_t	*intr_mapping_reg;
80	volatile uint64_t	mondo_vector;
81	int32_t			r_upaid = -1;
82	int32_t			slave = 0;
83	int32_t			portid;
84	int			len, ret;
85
86	if (((portid = ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
87	    DDI_PROP_DONTPASS, "upa-portid", -1)) != -1) ||
88	    ((portid = ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
89	    DDI_PROP_DONTPASS, "portid", -1)) != -1)) {
90		if (ddi_getprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
91		    "upa-interrupt-slave", 0) != 0) {
92
93			/* Give slave devices pri of 5. e.g. fb's */
94			hdlp->ih_pri = 5;
95		}
96
97		/*
98		 * Translate the interrupt property by stuffing in the
99		 * portid for those devices which have a portid.
100		 */
101		hdlp->ih_vector |= (UPAID_TO_IGN(portid) << 6);
102	}
103
104	/*
105	 * Hack to support the UPA slave devices before the 1275
106	 * support for imap was introduced.
107	 */
108	if (ddi_getproplen(DDI_DEV_T_ANY, dip, 0, "interrupt-map",
109	    &len) != DDI_PROP_SUCCESS && ddi_getprop(DDI_DEV_T_ANY,
110	    rdip, DDI_PROP_DONTPASS, "upa-interrupt-slave", 0) != 0 &&
111	    ddi_get_parent(rdip) == dip) {
112		slave = 1;
113
114		if ((r_upaid = ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
115		    DDI_PROP_DONTPASS, "upa-portid", -1)) != -1) {
116			extern uint64_t *get_intr_mapping_reg(int, int);
117
118			if ((intr_mapping_reg = get_intr_mapping_reg(
119			    r_upaid, 1)) == NULL)
120				return (DDI_FAILURE);
121		} else
122			return (DDI_FAILURE);
123	}
124
125	if ((ret = i_ddi_add_ivintr(hdlp)) != DDI_SUCCESS)
126		return (ret);
127
128	/*
129	 * Hack to support the UPA slave devices before the 1275
130	 * support for imap was introduced.
131	 */
132	if (slave) {
133		/*
134		 * Program the interrupt mapping register.
135		 * Interrupts from the slave UPA devices are
136		 * directed at the boot CPU until it is known
137		 * that they can be safely redirected while
138		 * running under load.
139		 */
140		mondo_vector = cpu0.cpu_id << IMR_TID_SHIFT;
141		mondo_vector |= (IMR_VALID | (uint64_t)hdlp->ih_vector);
142
143		/* Set the mapping register */
144		*intr_mapping_reg = mondo_vector;
145
146		/* Flush write buffers */
147		mondo_vector = *intr_mapping_reg;
148	}
149
150	return (ret);
151}
152
153/*
154 * rootnex_remove_intr_impl:
155 */
156/*ARGSUSED*/
157int
158rootnex_remove_intr_impl(dev_info_t *dip, dev_info_t *rdip,
159    ddi_intr_handle_impl_t *hdlp)
160{
161	int32_t		portid;
162	int		len;
163
164	if (((portid = ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
165	    DDI_PROP_DONTPASS, "upa-portid", -1)) != -1) ||
166	    ((portid = ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
167	    DDI_PROP_DONTPASS, "portid", -1)) != -1)) {
168		/*
169		 * Translate the interrupt property by stuffing in the
170		 * portid for those devices which have a portid.
171		 */
172		hdlp->ih_vector |= (UPAID_TO_IGN(portid) << 6);
173	}
174
175	/*
176	 * Hack to support the UPA slave devices before the 1275
177	 * support for imap was introduced.
178	 */
179	if (ddi_getproplen(DDI_DEV_T_ANY, dip, 0, "interrupt-map",
180	    &len) != DDI_PROP_SUCCESS && ddi_getprop(DDI_DEV_T_ANY,
181	    rdip, DDI_PROP_DONTPASS, "upa-interrupt-slave", 0) != 0) {
182		int32_t r_upaid = -1;
183
184		if ((r_upaid = ddi_prop_get_int(DDI_DEV_T_ANY, rdip,
185		    DDI_PROP_DONTPASS, "upa-portid", -1)) != -1 &&
186		    ddi_get_parent(rdip) == dip) {
187			volatile uint64_t *intr_mapping_reg;
188			volatile uint64_t flush_data;
189			extern uint64_t *get_intr_mapping_reg(int, int);
190
191			if ((intr_mapping_reg = get_intr_mapping_reg(
192			    r_upaid, 1)) == NULL)
193				return (DDI_SUCCESS);
194
195			/* Clear the mapping register */
196			*intr_mapping_reg = 0x0ull;
197
198			/* Flush write buffers */
199			flush_data = *intr_mapping_reg;
200#ifdef lint
201			flush_data = flush_data;
202#endif /* lint */
203		}
204	}
205
206	i_ddi_rem_ivintr(hdlp);
207
208	return (DDI_SUCCESS);
209}
210
211/*
212 * rootnex_get_intr_pri:
213 */
214/*ARGSUSED*/
215int
216rootnex_get_intr_pri(dev_info_t *dip, dev_info_t *rdip,
217    ddi_intr_handle_impl_t *hdlp)
218{
219	int	pri = hdlp->ih_pri;
220
221	if (ddi_prop_get_int(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
222	    "upa-portid", -1) != -1) {
223		if (ddi_getprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
224		    "upa-interrupt-slave", 0) != 0) {
225
226			/* Give slave devices pri of 5. e.g. fb's */
227			pri = 5;
228		}
229	}
230
231	return (pri);
232}
233
234int
235rootnex_ctl_reportdev_impl(dev_info_t *dev)
236{
237	struct regspec *rp;
238	char buf[80];
239	char *p = buf;
240	register int n;
241	int	portid;
242	int	nodeid;
243
244	(void) sprintf(p, "%s%d at root", ddi_driver_name(dev),
245	    ddi_get_instance(dev));
246	p += strlen(p);
247
248	if ((n = sparc_pd_getnreg(dev)) > 0) {
249		rp = sparc_pd_getreg(dev, 0);
250
251		(void) strcpy(p, ": ");
252		p += strlen(p);
253
254		/*
255		 * This stuff needs to be fixed correctly for the FFB
256		 * devices and the UPA add-on devices.
257		 */
258		portid = ddi_prop_get_int(DDI_DEV_T_ANY, dev,
259		    DDI_PROP_DONTPASS, "upa-portid", -1);
260		if (portid != -1)
261			(void) sprintf(p, "UPA 0x%x 0x%x%s",
262			    portid,
263			    rp->regspec_addr, (n > 1 ? "" : " ..."));
264		else {
265			portid = ddi_prop_get_int(DDI_DEV_T_ANY, dev,
266			    DDI_PROP_DONTPASS, "portid", -1);
267			nodeid = ddi_prop_get_int(DDI_DEV_T_ANY, dev,
268			    DDI_PROP_DONTPASS, "nodeid", -1);
269			if (portid == -1 && nodeid == -1)
270				printf("could not find portid "
271				    "or nodeid property in %s\n",
272				    DEVI(dev)->devi_node_name);
273
274			if (portid != -1)
275				(void) sprintf(p, "SAFARI 0x%x 0x%x%s",
276				    portid,
277				    rp->regspec_addr &
278				    root_phys_addr_lo_mask,
279				    (n > 1 ? "" : " ..."));
280			if (nodeid != -1)
281				(void) sprintf(p, "SSM Node %d", nodeid);
282		}
283		p += strlen(p);
284	}
285
286	/*
287	 * This is where we need to print out the interrupt specifications
288	 * for the FFB device and any UPA add-on devices.  Not sure how to
289	 * do this yet?
290	 */
291	cmn_err(CE_CONT, "?%s\n", buf);
292	return (DDI_SUCCESS);
293}
294
295int
296rootnex_name_child_impl(dev_info_t *child, char *name, int namelen)
297{
298	struct ddi_parent_private_data *pdptr;
299	int portid, nodeid;
300	char *node_name;
301	struct regspec *rp;
302
303	extern uint_t root_phys_addr_lo_mask;
304	extern void make_ddi_ppd(
305	    dev_info_t *, struct ddi_parent_private_data **);
306
307	/*
308	 * Fill in parent-private data and this function returns to us
309	 * an indication if it used "registers" to fill in the data.
310	 */
311	if (ddi_get_parent_data(child) == NULL) {
312		make_ddi_ppd(child, &pdptr);
313		ddi_set_parent_data(child, pdptr);
314	}
315
316	/*
317	 * No reg property, return null string as address (e.g. pseudo)
318	 */
319	name[0] = '\0';
320	if (sparc_pd_getnreg(child) == 0) {
321		return (DDI_SUCCESS);
322	}
323	rp = sparc_pd_getreg(child, 0);
324	ASSERT(rp != NULL);
325
326	/*
327	 * Create portid property for fhc node under root(/fhc).
328	 */
329	node_name = ddi_node_name(child);
330	if ((strcmp(node_name, "fhc") == 0) ||
331	    (strcmp(node_name, "mem-unit") == 0) ||
332	    (strcmp(node_name, "central") == 0)) {
333		portid = (rp->regspec_bustype >> 1) & 0x1f;
334
335		/*
336		 * The port-id must go on the hardware property list,
337		 * otherwise, initchild may fail.
338		 */
339		if (ndi_prop_update_int(DDI_DEV_T_NONE, child, "upa-portid",
340		    portid) != DDI_SUCCESS)
341			cmn_err(CE_WARN,
342			    "Error in creating upa-portid property for fhc.\n");
343	}
344
345	/*
346	 * Name node on behalf of child nexus.
347	 */
348	if (ddi_get_parent(child) != ddi_root_node()) {
349		(void) snprintf(name, namelen, "%x,%x",
350		    rp->regspec_bustype, rp->regspec_addr);
351		return (DDI_SUCCESS);
352	}
353
354	/*
355	 * On sun4u, the 'name' of children of the root node
356	 * is foo@<upa-mid>,<offset>, which is derived from,
357	 * but not identical to the physical address.
358	 */
359	portid = ddi_prop_get_int(DDI_DEV_T_ANY, child,
360	    DDI_PROP_DONTPASS, "upa-portid", -1);
361	if (portid == -1)
362		portid = ddi_prop_get_int(DDI_DEV_T_ANY, child,
363		    DDI_PROP_DONTPASS, "portid", -1);
364	nodeid = ddi_prop_get_int(DDI_DEV_T_ANY, child,
365	    DDI_PROP_DONTPASS, "nodeid", -1);
366
367	/*
368	 * Do not log message, to handle cases where OBP version
369	 * does not have "portid" property for the root i2c node.
370	 *
371	 * Platforms supporting root i2c node (potentially without
372	 * "portid" property) are :
373	 *	SunBlade 1500, SunBlade 2500, V240, V250
374	 */
375	if (portid == -1 && nodeid == -1 &&
376	    strncmp(node_name, "i2c", strlen("i2c")) != 0)
377		cmn_err(CE_WARN,
378		    "could not find portid or nodeid property in %s\n",
379		    DEVI(child)->devi_node_name);
380	if (nodeid != -1)
381		(void) snprintf(name, namelen, "%x,0", nodeid);
382	else
383		(void) snprintf(name, namelen, "%x,%x", portid,
384		    rp->regspec_addr & root_phys_addr_lo_mask);
385
386	return (DDI_SUCCESS);
387}
388
389int
390rootnex_ctl_initchild_impl(dev_info_t *dip)
391{
392	struct regspec *rp;
393	struct ddi_parent_private_data *pd;
394	char name[MAXNAMELEN];
395
396	extern struct ddi_parent_private_data *init_regspec_64(dev_info_t *dip);
397
398	/* Name the child */
399	(void) rootnex_name_child(dip, name, MAXNAMELEN);
400	ddi_set_name_addr(dip, name);
401
402	/*
403	 * Try to merge .conf node. If merge is successful, return
404	 * DDI_FAILURE to allow caller to remove this node.
405	 */
406	if (ndi_dev_is_persistent_node(dip) == 0 &&
407	    (ndi_merge_node(dip, rootnex_name_child) == DDI_SUCCESS)) {
408		(void) rootnex_ctl_uninitchild(dip);
409		return (DDI_FAILURE);
410	}
411
412	/*
413	 * If there are no "reg"s in the child node, return.
414	 */
415	pd = init_regspec_64(dip);
416	if ((pd == NULL) || (pd->par_nreg == 0))
417		return (DDI_SUCCESS);
418
419	/*
420	 * If this is a slave device sitting on the UPA, we assume that
421	 * This device can accept DMA accesses from other devices.  We need
422	 * to register this fact with the system by using the highest
423	 * and lowest physical pfns of the devices register space.  This
424	 * will then represent a physical block of addresses that are valid
425	 * for DMA accesses.
426	 */
427	if ((ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "upa-portid",
428	    -1) != -1) && ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
429	    "upa-interrupt-slave", 0)) {
430		pfn_t lopfn = (pfn_t)-1;
431		pfn_t hipfn = 0;
432		int i;
433		extern void pf_set_dmacapable(pfn_t, pfn_t);
434
435		/* Scan the devices highest and lowest physical pfns */
436		for (i = 0, rp = pd->par_reg; i < pd->par_nreg; i++, rp++) {
437			uint64_t addr;
438			pfn_t tmphipfn, tmplopfn;
439
440			addr = (unsigned long long)((unsigned long long)
441			    rp->regspec_bustype << 32);
442			addr |= (uint64_t)rp->regspec_addr;
443			tmplopfn = (pfn_t)(addr >> MMU_PAGESHIFT);
444			addr += (uint64_t)(rp->regspec_size - 1);
445			tmphipfn = (pfn_t)(addr >> MMU_PAGESHIFT);
446
447			hipfn = (tmphipfn > hipfn) ? tmphipfn : hipfn;
448			lopfn = (tmplopfn < lopfn) ? tmplopfn : lopfn;
449		}
450		pf_set_dmacapable(hipfn, lopfn);
451	}
452
453	return (DDI_SUCCESS);
454}
455
456void
457rootnex_ctl_uninitchild_impl(dev_info_t *dip)
458{
459	if ((ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "upa-portid",
460	    -1) != -1) && (ddi_getprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
461	    "upa-interrupt-slave", 0))) {
462		struct regspec *rp;
463		extern void pf_unset_dmacapable(pfn_t);
464		unsigned long long addr;
465		pfn_t pfn;
466		struct ddi_parent_private_data *pd;
467
468		pd = ddi_get_parent_data(dip);
469		ASSERT(pd != NULL);
470		rp = pd->par_reg;
471		addr = (unsigned long long) ((unsigned long long)
472		    rp->regspec_bustype << 32);
473		addr |= (unsigned long long) rp->regspec_addr;
474		pfn = (pfn_t)(addr >> MMU_PAGESHIFT);
475
476		pf_unset_dmacapable(pfn);
477	}
478}
479