1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <init.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <pci.h>
14 #include <asm/global_data.h>
15 #include <asm/io.h>
16 #include <dm/device-internal.h>
17 #include <dm/lists.h>
18 #include <dm/uclass-internal.h>
19 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
20 #include <asm/fsp/fsp_support.h>
21 #endif
22 #include <linux/delay.h>
23 #include "pci_internal.h"
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
pci_get_bus(int busnum,struct udevice ** busp)27 int pci_get_bus(int busnum, struct udevice **busp)
28 {
29 	int ret;
30 
31 	ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
32 
33 	/* Since buses may not be numbered yet try a little harder with bus 0 */
34 	if (ret == -ENODEV) {
35 		ret = uclass_first_device_err(UCLASS_PCI, busp);
36 		if (ret)
37 			return ret;
38 		ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
39 	}
40 
41 	return ret;
42 }
43 
pci_get_controller(struct udevice * dev)44 struct udevice *pci_get_controller(struct udevice *dev)
45 {
46 	while (device_is_on_pci_bus(dev))
47 		dev = dev->parent;
48 
49 	return dev;
50 }
51 
dm_pci_get_bdf(const struct udevice * dev)52 pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
53 {
54 	struct pci_child_plat *pplat = dev_get_parent_plat(dev);
55 	struct udevice *bus = dev->parent;
56 
57 	/*
58 	 * This error indicates that @dev is a device on an unprobed PCI bus.
59 	 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
60 	 * will produce a bad BDF>
61 	 *
62 	 * A common cause of this problem is that this function is called in the
63 	 * of_to_plat() method of @dev. Accessing the PCI bus in that
64 	 * method is not allowed, since it has not yet been probed. To fix this,
65 	 * move that access to the probe() method of @dev instead.
66 	 */
67 	if (!device_active(bus))
68 		log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
69 			bus->name);
70 	return PCI_ADD_BUS(dev_seq(bus), pplat->devfn);
71 }
72 
73 /**
74  * pci_get_bus_max() - returns the bus number of the last active bus
75  *
76  * @return last bus number, or -1 if no active buses
77  */
pci_get_bus_max(void)78 static int pci_get_bus_max(void)
79 {
80 	struct udevice *bus;
81 	struct uclass *uc;
82 	int ret = -1;
83 
84 	ret = uclass_get(UCLASS_PCI, &uc);
85 	uclass_foreach_dev(bus, uc) {
86 		if (dev_seq(bus) > ret)
87 			ret = dev_seq(bus);
88 	}
89 
90 	debug("%s: ret=%d\n", __func__, ret);
91 
92 	return ret;
93 }
94 
pci_last_busno(void)95 int pci_last_busno(void)
96 {
97 	return pci_get_bus_max();
98 }
99 
pci_get_ff(enum pci_size_t size)100 int pci_get_ff(enum pci_size_t size)
101 {
102 	switch (size) {
103 	case PCI_SIZE_8:
104 		return 0xff;
105 	case PCI_SIZE_16:
106 		return 0xffff;
107 	default:
108 		return 0xffffffff;
109 	}
110 }
111 
pci_dev_find_ofnode(struct udevice * bus,phys_addr_t bdf,ofnode * rnode)112 static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
113 				ofnode *rnode)
114 {
115 	struct fdt_pci_addr addr;
116 	ofnode node;
117 	int ret;
118 
119 	dev_for_each_subnode(node, bus) {
120 		ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
121 					   &addr);
122 		if (ret)
123 			continue;
124 
125 		if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
126 			continue;
127 
128 		*rnode = node;
129 		break;
130 	}
131 };
132 
pci_bus_find_devfn(const struct udevice * bus,pci_dev_t find_devfn,struct udevice ** devp)133 int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
134 		       struct udevice **devp)
135 {
136 	struct udevice *dev;
137 
138 	for (device_find_first_child(bus, &dev);
139 	     dev;
140 	     device_find_next_child(&dev)) {
141 		struct pci_child_plat *pplat;
142 
143 		pplat = dev_get_parent_plat(dev);
144 		if (pplat && pplat->devfn == find_devfn) {
145 			*devp = dev;
146 			return 0;
147 		}
148 	}
149 
150 	return -ENODEV;
151 }
152 
dm_pci_bus_find_bdf(pci_dev_t bdf,struct udevice ** devp)153 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
154 {
155 	struct udevice *bus;
156 	int ret;
157 
158 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
159 	if (ret)
160 		return ret;
161 	return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
162 }
163 
pci_device_matches_ids(struct udevice * dev,struct pci_device_id * ids)164 static int pci_device_matches_ids(struct udevice *dev,
165 				  struct pci_device_id *ids)
166 {
167 	struct pci_child_plat *pplat;
168 	int i;
169 
170 	pplat = dev_get_parent_plat(dev);
171 	if (!pplat)
172 		return -EINVAL;
173 	for (i = 0; ids[i].vendor != 0; i++) {
174 		if (pplat->vendor == ids[i].vendor &&
175 		    pplat->device == ids[i].device)
176 			return i;
177 	}
178 
179 	return -EINVAL;
180 }
181 
pci_bus_find_devices(struct udevice * bus,struct pci_device_id * ids,int * indexp,struct udevice ** devp)182 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
183 			 int *indexp, struct udevice **devp)
184 {
185 	struct udevice *dev;
186 
187 	/* Scan all devices on this bus */
188 	for (device_find_first_child(bus, &dev);
189 	     dev;
190 	     device_find_next_child(&dev)) {
191 		if (pci_device_matches_ids(dev, ids) >= 0) {
192 			if ((*indexp)-- <= 0) {
193 				*devp = dev;
194 				return 0;
195 			}
196 		}
197 	}
198 
199 	return -ENODEV;
200 }
201 
pci_find_device_id(struct pci_device_id * ids,int index,struct udevice ** devp)202 int pci_find_device_id(struct pci_device_id *ids, int index,
203 		       struct udevice **devp)
204 {
205 	struct udevice *bus;
206 
207 	/* Scan all known buses */
208 	for (uclass_first_device(UCLASS_PCI, &bus);
209 	     bus;
210 	     uclass_next_device(&bus)) {
211 		if (!pci_bus_find_devices(bus, ids, &index, devp))
212 			return 0;
213 	}
214 	*devp = NULL;
215 
216 	return -ENODEV;
217 }
218 
dm_pci_bus_find_device(struct udevice * bus,unsigned int vendor,unsigned int device,int * indexp,struct udevice ** devp)219 static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
220 				  unsigned int device, int *indexp,
221 				  struct udevice **devp)
222 {
223 	struct pci_child_plat *pplat;
224 	struct udevice *dev;
225 
226 	for (device_find_first_child(bus, &dev);
227 	     dev;
228 	     device_find_next_child(&dev)) {
229 		pplat = dev_get_parent_plat(dev);
230 		if (pplat->vendor == vendor && pplat->device == device) {
231 			if (!(*indexp)--) {
232 				*devp = dev;
233 				return 0;
234 			}
235 		}
236 	}
237 
238 	return -ENODEV;
239 }
240 
dm_pci_find_device(unsigned int vendor,unsigned int device,int index,struct udevice ** devp)241 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
242 		       struct udevice **devp)
243 {
244 	struct udevice *bus;
245 
246 	/* Scan all known buses */
247 	for (uclass_first_device(UCLASS_PCI, &bus);
248 	     bus;
249 	     uclass_next_device(&bus)) {
250 		if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
251 			return device_probe(*devp);
252 	}
253 	*devp = NULL;
254 
255 	return -ENODEV;
256 }
257 
dm_pci_find_class(uint find_class,int index,struct udevice ** devp)258 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
259 {
260 	struct udevice *dev;
261 
262 	/* Scan all known buses */
263 	for (pci_find_first_device(&dev);
264 	     dev;
265 	     pci_find_next_device(&dev)) {
266 		struct pci_child_plat *pplat = dev_get_parent_plat(dev);
267 
268 		if (pplat->class == find_class && !index--) {
269 			*devp = dev;
270 			return device_probe(*devp);
271 		}
272 	}
273 	*devp = NULL;
274 
275 	return -ENODEV;
276 }
277 
pci_bus_write_config(struct udevice * bus,pci_dev_t bdf,int offset,unsigned long value,enum pci_size_t size)278 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
279 			 unsigned long value, enum pci_size_t size)
280 {
281 	struct dm_pci_ops *ops;
282 
283 	ops = pci_get_ops(bus);
284 	if (!ops->write_config)
285 		return -ENOSYS;
286 	return ops->write_config(bus, bdf, offset, value, size);
287 }
288 
pci_bus_clrset_config32(struct udevice * bus,pci_dev_t bdf,int offset,u32 clr,u32 set)289 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
290 			    u32 clr, u32 set)
291 {
292 	ulong val;
293 	int ret;
294 
295 	ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
296 	if (ret)
297 		return ret;
298 	val &= ~clr;
299 	val |= set;
300 
301 	return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
302 }
303 
pci_write_config(pci_dev_t bdf,int offset,unsigned long value,enum pci_size_t size)304 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
305 		     enum pci_size_t size)
306 {
307 	struct udevice *bus;
308 	int ret;
309 
310 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
311 	if (ret)
312 		return ret;
313 
314 	return pci_bus_write_config(bus, bdf, offset, value, size);
315 }
316 
dm_pci_write_config(struct udevice * dev,int offset,unsigned long value,enum pci_size_t size)317 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
318 			enum pci_size_t size)
319 {
320 	struct udevice *bus;
321 
322 	for (bus = dev; device_is_on_pci_bus(bus);)
323 		bus = bus->parent;
324 	return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
325 				    size);
326 }
327 
pci_write_config32(pci_dev_t bdf,int offset,u32 value)328 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
329 {
330 	return pci_write_config(bdf, offset, value, PCI_SIZE_32);
331 }
332 
pci_write_config16(pci_dev_t bdf,int offset,u16 value)333 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
334 {
335 	return pci_write_config(bdf, offset, value, PCI_SIZE_16);
336 }
337 
pci_write_config8(pci_dev_t bdf,int offset,u8 value)338 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
339 {
340 	return pci_write_config(bdf, offset, value, PCI_SIZE_8);
341 }
342 
dm_pci_write_config8(struct udevice * dev,int offset,u8 value)343 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
344 {
345 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
346 }
347 
dm_pci_write_config16(struct udevice * dev,int offset,u16 value)348 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
349 {
350 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
351 }
352 
dm_pci_write_config32(struct udevice * dev,int offset,u32 value)353 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
354 {
355 	return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
356 }
357 
pci_bus_read_config(const struct udevice * bus,pci_dev_t bdf,int offset,unsigned long * valuep,enum pci_size_t size)358 int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
359 			unsigned long *valuep, enum pci_size_t size)
360 {
361 	struct dm_pci_ops *ops;
362 
363 	ops = pci_get_ops(bus);
364 	if (!ops->read_config)
365 		return -ENOSYS;
366 	return ops->read_config(bus, bdf, offset, valuep, size);
367 }
368 
pci_read_config(pci_dev_t bdf,int offset,unsigned long * valuep,enum pci_size_t size)369 int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
370 		    enum pci_size_t size)
371 {
372 	struct udevice *bus;
373 	int ret;
374 
375 	ret = pci_get_bus(PCI_BUS(bdf), &bus);
376 	if (ret)
377 		return ret;
378 
379 	return pci_bus_read_config(bus, bdf, offset, valuep, size);
380 }
381 
dm_pci_read_config(const struct udevice * dev,int offset,unsigned long * valuep,enum pci_size_t size)382 int dm_pci_read_config(const struct udevice *dev, int offset,
383 		       unsigned long *valuep, enum pci_size_t size)
384 {
385 	const struct udevice *bus;
386 
387 	for (bus = dev; device_is_on_pci_bus(bus);)
388 		bus = bus->parent;
389 	return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
390 				   size);
391 }
392 
pci_read_config32(pci_dev_t bdf,int offset,u32 * valuep)393 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
394 {
395 	unsigned long value;
396 	int ret;
397 
398 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
399 	if (ret)
400 		return ret;
401 	*valuep = value;
402 
403 	return 0;
404 }
405 
pci_read_config16(pci_dev_t bdf,int offset,u16 * valuep)406 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
407 {
408 	unsigned long value;
409 	int ret;
410 
411 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
412 	if (ret)
413 		return ret;
414 	*valuep = value;
415 
416 	return 0;
417 }
418 
pci_read_config8(pci_dev_t bdf,int offset,u8 * valuep)419 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
420 {
421 	unsigned long value;
422 	int ret;
423 
424 	ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
425 	if (ret)
426 		return ret;
427 	*valuep = value;
428 
429 	return 0;
430 }
431 
dm_pci_read_config8(const struct udevice * dev,int offset,u8 * valuep)432 int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
433 {
434 	unsigned long value;
435 	int ret;
436 
437 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
438 	if (ret)
439 		return ret;
440 	*valuep = value;
441 
442 	return 0;
443 }
444 
dm_pci_read_config16(const struct udevice * dev,int offset,u16 * valuep)445 int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
446 {
447 	unsigned long value;
448 	int ret;
449 
450 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
451 	if (ret)
452 		return ret;
453 	*valuep = value;
454 
455 	return 0;
456 }
457 
dm_pci_read_config32(const struct udevice * dev,int offset,u32 * valuep)458 int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
459 {
460 	unsigned long value;
461 	int ret;
462 
463 	ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
464 	if (ret)
465 		return ret;
466 	*valuep = value;
467 
468 	return 0;
469 }
470 
dm_pci_clrset_config8(struct udevice * dev,int offset,u32 clr,u32 set)471 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
472 {
473 	u8 val;
474 	int ret;
475 
476 	ret = dm_pci_read_config8(dev, offset, &val);
477 	if (ret)
478 		return ret;
479 	val &= ~clr;
480 	val |= set;
481 
482 	return dm_pci_write_config8(dev, offset, val);
483 }
484 
dm_pci_clrset_config16(struct udevice * dev,int offset,u32 clr,u32 set)485 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
486 {
487 	u16 val;
488 	int ret;
489 
490 	ret = dm_pci_read_config16(dev, offset, &val);
491 	if (ret)
492 		return ret;
493 	val &= ~clr;
494 	val |= set;
495 
496 	return dm_pci_write_config16(dev, offset, val);
497 }
498 
dm_pci_clrset_config32(struct udevice * dev,int offset,u32 clr,u32 set)499 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
500 {
501 	u32 val;
502 	int ret;
503 
504 	ret = dm_pci_read_config32(dev, offset, &val);
505 	if (ret)
506 		return ret;
507 	val &= ~clr;
508 	val |= set;
509 
510 	return dm_pci_write_config32(dev, offset, val);
511 }
512 
set_vga_bridge_bits(struct udevice * dev)513 static void set_vga_bridge_bits(struct udevice *dev)
514 {
515 	struct udevice *parent = dev->parent;
516 	u16 bc;
517 
518 	while (dev_seq(parent) != 0) {
519 		dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
520 		bc |= PCI_BRIDGE_CTL_VGA;
521 		dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
522 		parent = parent->parent;
523 	}
524 }
525 
pci_auto_config_devices(struct udevice * bus)526 int pci_auto_config_devices(struct udevice *bus)
527 {
528 	struct pci_controller *hose = dev_get_uclass_priv(bus);
529 	struct pci_child_plat *pplat;
530 	unsigned int sub_bus;
531 	struct udevice *dev;
532 	int ret;
533 
534 	sub_bus = dev_seq(bus);
535 	debug("%s: start\n", __func__);
536 	pciauto_config_init(hose);
537 	for (ret = device_find_first_child(bus, &dev);
538 	     !ret && dev;
539 	     ret = device_find_next_child(&dev)) {
540 		unsigned int max_bus;
541 		int ret;
542 
543 		debug("%s: device %s\n", __func__, dev->name);
544 		if (dev_has_ofnode(dev) &&
545 		    dev_read_bool(dev, "pci,no-autoconfig"))
546 			continue;
547 		ret = dm_pciauto_config_device(dev);
548 		if (ret < 0)
549 			return log_msg_ret("auto", ret);
550 		max_bus = ret;
551 		sub_bus = max(sub_bus, max_bus);
552 
553 		pplat = dev_get_parent_plat(dev);
554 		if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
555 			set_vga_bridge_bits(dev);
556 	}
557 	debug("%s: done\n", __func__);
558 
559 	return log_msg_ret("sub", sub_bus);
560 }
561 
pci_generic_mmap_write_config(const struct udevice * bus,int (* addr_f)(const struct udevice * bus,pci_dev_t bdf,uint offset,void ** addrp),pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)562 int pci_generic_mmap_write_config(
563 	const struct udevice *bus,
564 	int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
565 		      void **addrp),
566 	pci_dev_t bdf,
567 	uint offset,
568 	ulong value,
569 	enum pci_size_t size)
570 {
571 	void *address;
572 
573 	if (addr_f(bus, bdf, offset, &address) < 0)
574 		return 0;
575 
576 	switch (size) {
577 	case PCI_SIZE_8:
578 		writeb(value, address);
579 		return 0;
580 	case PCI_SIZE_16:
581 		writew(value, address);
582 		return 0;
583 	case PCI_SIZE_32:
584 		writel(value, address);
585 		return 0;
586 	default:
587 		return -EINVAL;
588 	}
589 }
590 
pci_generic_mmap_read_config(const struct udevice * bus,int (* addr_f)(const struct udevice * bus,pci_dev_t bdf,uint offset,void ** addrp),pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)591 int pci_generic_mmap_read_config(
592 	const struct udevice *bus,
593 	int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
594 		      void **addrp),
595 	pci_dev_t bdf,
596 	uint offset,
597 	ulong *valuep,
598 	enum pci_size_t size)
599 {
600 	void *address;
601 
602 	if (addr_f(bus, bdf, offset, &address) < 0) {
603 		*valuep = pci_get_ff(size);
604 		return 0;
605 	}
606 
607 	switch (size) {
608 	case PCI_SIZE_8:
609 		*valuep = readb(address);
610 		return 0;
611 	case PCI_SIZE_16:
612 		*valuep = readw(address);
613 		return 0;
614 	case PCI_SIZE_32:
615 		*valuep = readl(address);
616 		return 0;
617 	default:
618 		return -EINVAL;
619 	}
620 }
621 
dm_pci_hose_probe_bus(struct udevice * bus)622 int dm_pci_hose_probe_bus(struct udevice *bus)
623 {
624 	int sub_bus;
625 	int ret;
626 	int ea_pos;
627 	u8 reg;
628 
629 	debug("%s\n", __func__);
630 
631 	ea_pos = dm_pci_find_capability(bus, PCI_CAP_ID_EA);
632 	if (ea_pos) {
633 		dm_pci_read_config8(bus, ea_pos + sizeof(u32) + sizeof(u8),
634 				    &reg);
635 		sub_bus = reg;
636 	} else {
637 		sub_bus = pci_get_bus_max() + 1;
638 	}
639 	debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
640 	dm_pciauto_prescan_setup_bridge(bus, sub_bus);
641 
642 	ret = device_probe(bus);
643 	if (ret) {
644 		debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
645 		      ret);
646 		return log_msg_ret("probe", ret);
647 	}
648 
649 	dm_pciauto_postscan_setup_bridge(bus, sub_bus);
650 
651 	return sub_bus;
652 }
653 
654 /**
655  * pci_match_one_device - Tell if a PCI device structure has a matching
656  *                        PCI device id structure
657  * @id: single PCI device id structure to match
658  * @find: the PCI device id structure to match against
659  *
660  * Returns true if the finding pci_device_id structure matched or false if
661  * there is no match.
662  */
pci_match_one_id(const struct pci_device_id * id,const struct pci_device_id * find)663 static bool pci_match_one_id(const struct pci_device_id *id,
664 			     const struct pci_device_id *find)
665 {
666 	if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
667 	    (id->device == PCI_ANY_ID || id->device == find->device) &&
668 	    (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
669 	    (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
670 	    !((id->class ^ find->class) & id->class_mask))
671 		return true;
672 
673 	return false;
674 }
675 
676 /**
677  * pci_find_and_bind_driver() - Find and bind the right PCI driver
678  *
679  * This only looks at certain fields in the descriptor.
680  *
681  * @parent:	Parent bus
682  * @find_id:	Specification of the driver to find
683  * @bdf:	Bus/device/function addreess - see PCI_BDF()
684  * @devp:	Returns a pointer to the device created
685  * @return 0 if OK, -EPERM if the device is not needed before relocation and
686  *	   therefore was not created, other -ve value on error
687  */
pci_find_and_bind_driver(struct udevice * parent,struct pci_device_id * find_id,pci_dev_t bdf,struct udevice ** devp)688 static int pci_find_and_bind_driver(struct udevice *parent,
689 				    struct pci_device_id *find_id,
690 				    pci_dev_t bdf, struct udevice **devp)
691 {
692 	struct pci_driver_entry *start, *entry;
693 	ofnode node = ofnode_null();
694 	const char *drv;
695 	int n_ents;
696 	int ret;
697 	char name[30], *str;
698 	bool bridge;
699 
700 	*devp = NULL;
701 
702 	debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
703 	      find_id->vendor, find_id->device);
704 
705 	/* Determine optional OF node */
706 	if (ofnode_valid(dev_ofnode(parent)))
707 		pci_dev_find_ofnode(parent, bdf, &node);
708 
709 	if (ofnode_valid(node) && !ofnode_is_available(node)) {
710 		debug("%s: Ignoring disabled device\n", __func__);
711 		return log_msg_ret("dis", -EPERM);
712 	}
713 
714 	start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
715 	n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
716 	for (entry = start; entry != start + n_ents; entry++) {
717 		const struct pci_device_id *id;
718 		struct udevice *dev;
719 		const struct driver *drv;
720 
721 		for (id = entry->match;
722 		     id->vendor || id->subvendor || id->class_mask;
723 		     id++) {
724 			if (!pci_match_one_id(id, find_id))
725 				continue;
726 
727 			drv = entry->driver;
728 
729 			/*
730 			 * In the pre-relocation phase, we only bind devices
731 			 * whose driver has the DM_FLAG_PRE_RELOC set, to save
732 			 * precious memory space as on some platforms as that
733 			 * space is pretty limited (ie: using Cache As RAM).
734 			 */
735 			if (!(gd->flags & GD_FLG_RELOC) &&
736 			    !(drv->flags & DM_FLAG_PRE_RELOC))
737 				return log_msg_ret("pre", -EPERM);
738 
739 			/*
740 			 * We could pass the descriptor to the driver as
741 			 * plat (instead of NULL) and allow its bind()
742 			 * method to return -ENOENT if it doesn't support this
743 			 * device. That way we could continue the search to
744 			 * find another driver. For now this doesn't seem
745 			 * necesssary, so just bind the first match.
746 			 */
747 			ret = device_bind(parent, drv, drv->name, NULL, node,
748 					  &dev);
749 			if (ret)
750 				goto error;
751 			debug("%s: Match found: %s\n", __func__, drv->name);
752 			dev->driver_data = id->driver_data;
753 			*devp = dev;
754 			return 0;
755 		}
756 	}
757 
758 	bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
759 	/*
760 	 * In the pre-relocation phase, we only bind bridge devices to save
761 	 * precious memory space as on some platforms as that space is pretty
762 	 * limited (ie: using Cache As RAM).
763 	 */
764 	if (!(gd->flags & GD_FLG_RELOC) && !bridge)
765 		return log_msg_ret("notbr", -EPERM);
766 
767 	/* Bind a generic driver so that the device can be used */
768 	sprintf(name, "pci_%x:%x.%x", dev_seq(parent), PCI_DEV(bdf),
769 		PCI_FUNC(bdf));
770 	str = strdup(name);
771 	if (!str)
772 		return -ENOMEM;
773 	drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
774 
775 	ret = device_bind_driver_to_node(parent, drv, str, node, devp);
776 	if (ret) {
777 		debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
778 		free(str);
779 		return ret;
780 	}
781 	debug("%s: No match found: bound generic driver instead\n", __func__);
782 
783 	return 0;
784 
785 error:
786 	debug("%s: No match found: error %d\n", __func__, ret);
787 	return ret;
788 }
789 
pci_bind_bus_devices(struct udevice * bus)790 int pci_bind_bus_devices(struct udevice *bus)
791 {
792 	ulong vendor, device;
793 	ulong header_type;
794 	pci_dev_t bdf, end;
795 	bool found_multi;
796 	int ari_off;
797 	int ret;
798 
799 	found_multi = false;
800 	end = PCI_BDF(dev_seq(bus), PCI_MAX_PCI_DEVICES - 1,
801 		      PCI_MAX_PCI_FUNCTIONS - 1);
802 	for (bdf = PCI_BDF(dev_seq(bus), 0, 0); bdf <= end;
803 	     bdf += PCI_BDF(0, 0, 1)) {
804 		struct pci_child_plat *pplat;
805 		struct udevice *dev;
806 		ulong class;
807 
808 		if (!PCI_FUNC(bdf))
809 			found_multi = false;
810 		if (PCI_FUNC(bdf) && !found_multi)
811 			continue;
812 
813 		/* Check only the first access, we don't expect problems */
814 		ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
815 					  PCI_SIZE_16);
816 		if (ret)
817 			goto error;
818 
819 		if (vendor == 0xffff || vendor == 0x0000)
820 			continue;
821 
822 		pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
823 				    &header_type, PCI_SIZE_8);
824 
825 		if (!PCI_FUNC(bdf))
826 			found_multi = header_type & 0x80;
827 
828 		debug("%s: bus %d/%s: found device %x, function %d", __func__,
829 		      dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
830 		pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
831 				    PCI_SIZE_16);
832 		pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
833 				    PCI_SIZE_32);
834 		class >>= 8;
835 
836 		/* Find this device in the device tree */
837 		ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
838 		debug(": find ret=%d\n", ret);
839 
840 		/* If nothing in the device tree, bind a device */
841 		if (ret == -ENODEV) {
842 			struct pci_device_id find_id;
843 			ulong val;
844 
845 			memset(&find_id, '\0', sizeof(find_id));
846 			find_id.vendor = vendor;
847 			find_id.device = device;
848 			find_id.class = class;
849 			if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
850 				pci_bus_read_config(bus, bdf,
851 						    PCI_SUBSYSTEM_VENDOR_ID,
852 						    &val, PCI_SIZE_32);
853 				find_id.subvendor = val & 0xffff;
854 				find_id.subdevice = val >> 16;
855 			}
856 			ret = pci_find_and_bind_driver(bus, &find_id, bdf,
857 						       &dev);
858 		}
859 		if (ret == -EPERM)
860 			continue;
861 		else if (ret)
862 			return ret;
863 
864 		/* Update the platform data */
865 		pplat = dev_get_parent_plat(dev);
866 		pplat->devfn = PCI_MASK_BUS(bdf);
867 		pplat->vendor = vendor;
868 		pplat->device = device;
869 		pplat->class = class;
870 
871 		if (IS_ENABLED(CONFIG_PCI_ARID)) {
872 			ari_off = dm_pci_find_ext_capability(dev,
873 							     PCI_EXT_CAP_ID_ARI);
874 			if (ari_off) {
875 				u16 ari_cap;
876 
877 				/*
878 				 * Read Next Function number in ARI Cap
879 				 * Register
880 				 */
881 				dm_pci_read_config16(dev, ari_off + 4,
882 						     &ari_cap);
883 				/*
884 				 * Update next scan on this function number,
885 				 * subtract 1 in BDF to satisfy loop increment.
886 				 */
887 				if (ari_cap & 0xff00) {
888 					bdf = PCI_BDF(PCI_BUS(bdf),
889 						      PCI_DEV(ari_cap),
890 						      PCI_FUNC(ari_cap));
891 					bdf = bdf - 0x100;
892 				}
893 			}
894 		}
895 	}
896 
897 	return 0;
898 error:
899 	printf("Cannot read bus configuration: %d\n", ret);
900 
901 	return ret;
902 }
903 
decode_regions(struct pci_controller * hose,ofnode parent_node,ofnode node)904 static void decode_regions(struct pci_controller *hose, ofnode parent_node,
905 			   ofnode node)
906 {
907 	int pci_addr_cells, addr_cells, size_cells;
908 	int cells_per_record;
909 	struct bd_info *bd;
910 	const u32 *prop;
911 	int max_regions;
912 	int len;
913 	int i;
914 
915 	prop = ofnode_get_property(node, "ranges", &len);
916 	if (!prop) {
917 		debug("%s: Cannot decode regions\n", __func__);
918 		return;
919 	}
920 
921 	pci_addr_cells = ofnode_read_simple_addr_cells(node);
922 	addr_cells = ofnode_read_simple_addr_cells(parent_node);
923 	size_cells = ofnode_read_simple_size_cells(node);
924 
925 	/* PCI addresses are always 3-cells */
926 	len /= sizeof(u32);
927 	cells_per_record = pci_addr_cells + addr_cells + size_cells;
928 	hose->region_count = 0;
929 	debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
930 	      cells_per_record);
931 
932 	/* Dynamically allocate the regions array */
933 	max_regions = len / cells_per_record + CONFIG_NR_DRAM_BANKS;
934 	hose->regions = (struct pci_region *)
935 		calloc(1, max_regions * sizeof(struct pci_region));
936 
937 	for (i = 0; i < max_regions; i++, len -= cells_per_record) {
938 		u64 pci_addr, addr, size;
939 		int space_code;
940 		u32 flags;
941 		int type;
942 		int pos;
943 
944 		if (len < cells_per_record)
945 			break;
946 		flags = fdt32_to_cpu(prop[0]);
947 		space_code = (flags >> 24) & 3;
948 		pci_addr = fdtdec_get_number(prop + 1, 2);
949 		prop += pci_addr_cells;
950 		addr = fdtdec_get_number(prop, addr_cells);
951 		prop += addr_cells;
952 		size = fdtdec_get_number(prop, size_cells);
953 		prop += size_cells;
954 		debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
955 		      __func__, hose->region_count, pci_addr, addr, size, space_code);
956 		if (space_code & 2) {
957 			type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
958 					PCI_REGION_MEM;
959 		} else if (space_code & 1) {
960 			type = PCI_REGION_IO;
961 		} else {
962 			continue;
963 		}
964 
965 		if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
966 		    type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
967 			debug(" - beyond the 32-bit boundary, ignoring\n");
968 			continue;
969 		}
970 
971 		pos = -1;
972 		if (!IS_ENABLED(CONFIG_PCI_REGION_MULTI_ENTRY)) {
973 			for (i = 0; i < hose->region_count; i++) {
974 				if (hose->regions[i].flags == type)
975 					pos = i;
976 			}
977 		}
978 
979 		if (pos == -1)
980 			pos = hose->region_count++;
981 		debug(" - type=%d, pos=%d\n", type, pos);
982 		pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
983 	}
984 
985 	/* Add a region for our local memory */
986 	bd = gd->bd;
987 	if (!bd)
988 		return;
989 
990 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
991 		if (bd->bi_dram[i].size) {
992 			pci_set_region(hose->regions + hose->region_count++,
993 				       bd->bi_dram[i].start,
994 				       bd->bi_dram[i].start,
995 				       bd->bi_dram[i].size,
996 				       PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
997 		}
998 	}
999 
1000 	return;
1001 }
1002 
pci_uclass_pre_probe(struct udevice * bus)1003 static int pci_uclass_pre_probe(struct udevice *bus)
1004 {
1005 	struct pci_controller *hose;
1006 	struct uclass *uc;
1007 	int ret;
1008 
1009 	debug("%s, bus=%d/%s, parent=%s\n", __func__, dev_seq(bus), bus->name,
1010 	      bus->parent->name);
1011 	hose = dev_get_uclass_priv(bus);
1012 
1013 	/*
1014 	 * Set the sequence number, if device_bind() doesn't. We want control
1015 	 * of this so that numbers are allocated as devices are probed. That
1016 	 * ensures that sub-bus numbered is correct (sub-buses must get numbers
1017 	 * higher than their parents)
1018 	 */
1019 	if (dev_seq(bus) == -1) {
1020 		ret = uclass_get(UCLASS_PCI, &uc);
1021 		if (ret)
1022 			return ret;
1023 		bus->seq_ = uclass_find_next_free_seq(uc);
1024 	}
1025 
1026 	/* For bridges, use the top-level PCI controller */
1027 	if (!device_is_on_pci_bus(bus)) {
1028 		hose->ctlr = bus;
1029 		decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
1030 	} else {
1031 		struct pci_controller *parent_hose;
1032 
1033 		parent_hose = dev_get_uclass_priv(bus->parent);
1034 		hose->ctlr = parent_hose->bus;
1035 	}
1036 
1037 	hose->bus = bus;
1038 	hose->first_busno = dev_seq(bus);
1039 	hose->last_busno = dev_seq(bus);
1040 	if (dev_has_ofnode(bus)) {
1041 		hose->skip_auto_config_until_reloc =
1042 			dev_read_bool(bus,
1043 				      "u-boot,skip-auto-config-until-reloc");
1044 	}
1045 
1046 	return 0;
1047 }
1048 
pci_uclass_post_probe(struct udevice * bus)1049 static int pci_uclass_post_probe(struct udevice *bus)
1050 {
1051 	struct pci_controller *hose = dev_get_uclass_priv(bus);
1052 	int ret;
1053 
1054 	debug("%s: probing bus %d\n", __func__, dev_seq(bus));
1055 	ret = pci_bind_bus_devices(bus);
1056 	if (ret)
1057 		return log_msg_ret("bind", ret);
1058 
1059 	if (CONFIG_IS_ENABLED(PCI_PNP) && ll_boot_init() &&
1060 	    (!hose->skip_auto_config_until_reloc ||
1061 	     (gd->flags & GD_FLG_RELOC))) {
1062 		ret = pci_auto_config_devices(bus);
1063 		if (ret < 0)
1064 			return log_msg_ret("cfg", ret);
1065 	}
1066 
1067 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1068 	/*
1069 	 * Per Intel FSP specification, we should call FSP notify API to
1070 	 * inform FSP that PCI enumeration has been done so that FSP will
1071 	 * do any necessary initialization as required by the chipset's
1072 	 * BIOS Writer's Guide (BWG).
1073 	 *
1074 	 * Unfortunately we have to put this call here as with driver model,
1075 	 * the enumeration is all done on a lazy basis as needed, so until
1076 	 * something is touched on PCI it won't happen.
1077 	 *
1078 	 * Note we only call this 1) after U-Boot is relocated, and 2)
1079 	 * root bus has finished probing.
1080 	 */
1081 	if ((gd->flags & GD_FLG_RELOC) && dev_seq(bus) == 0 && ll_boot_init()) {
1082 		ret = fsp_init_phase_pci();
1083 		if (ret)
1084 			return log_msg_ret("fsp", ret);
1085 	}
1086 #endif
1087 
1088 	return 0;
1089 }
1090 
pci_uclass_child_post_bind(struct udevice * dev)1091 static int pci_uclass_child_post_bind(struct udevice *dev)
1092 {
1093 	struct pci_child_plat *pplat;
1094 
1095 	if (!dev_has_ofnode(dev))
1096 		return 0;
1097 
1098 	pplat = dev_get_parent_plat(dev);
1099 
1100 	/* Extract vendor id and device id if available */
1101 	ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1102 
1103 	/* Extract the devfn from fdt_pci_addr */
1104 	pplat->devfn = pci_get_devfn(dev);
1105 
1106 	return 0;
1107 }
1108 
pci_bridge_read_config(const struct udevice * bus,pci_dev_t bdf,uint offset,ulong * valuep,enum pci_size_t size)1109 static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
1110 				  uint offset, ulong *valuep,
1111 				  enum pci_size_t size)
1112 {
1113 	struct pci_controller *hose = dev_get_uclass_priv(bus);
1114 
1115 	return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1116 }
1117 
pci_bridge_write_config(struct udevice * bus,pci_dev_t bdf,uint offset,ulong value,enum pci_size_t size)1118 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1119 				   uint offset, ulong value,
1120 				   enum pci_size_t size)
1121 {
1122 	struct pci_controller *hose = dev_get_uclass_priv(bus);
1123 
1124 	return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1125 }
1126 
skip_to_next_device(struct udevice * bus,struct udevice ** devp)1127 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1128 {
1129 	struct udevice *dev;
1130 	int ret = 0;
1131 
1132 	/*
1133 	 * Scan through all the PCI controllers. On x86 there will only be one
1134 	 * but that is not necessarily true on other hardware.
1135 	 */
1136 	do {
1137 		device_find_first_child(bus, &dev);
1138 		if (dev) {
1139 			*devp = dev;
1140 			return 0;
1141 		}
1142 		ret = uclass_next_device(&bus);
1143 		if (ret)
1144 			return ret;
1145 	} while (bus);
1146 
1147 	return 0;
1148 }
1149 
pci_find_next_device(struct udevice ** devp)1150 int pci_find_next_device(struct udevice **devp)
1151 {
1152 	struct udevice *child = *devp;
1153 	struct udevice *bus = child->parent;
1154 	int ret;
1155 
1156 	/* First try all the siblings */
1157 	*devp = NULL;
1158 	while (child) {
1159 		device_find_next_child(&child);
1160 		if (child) {
1161 			*devp = child;
1162 			return 0;
1163 		}
1164 	}
1165 
1166 	/* We ran out of siblings. Try the next bus */
1167 	ret = uclass_next_device(&bus);
1168 	if (ret)
1169 		return ret;
1170 
1171 	return bus ? skip_to_next_device(bus, devp) : 0;
1172 }
1173 
pci_find_first_device(struct udevice ** devp)1174 int pci_find_first_device(struct udevice **devp)
1175 {
1176 	struct udevice *bus;
1177 	int ret;
1178 
1179 	*devp = NULL;
1180 	ret = uclass_first_device(UCLASS_PCI, &bus);
1181 	if (ret)
1182 		return ret;
1183 
1184 	return skip_to_next_device(bus, devp);
1185 }
1186 
pci_conv_32_to_size(ulong value,uint offset,enum pci_size_t size)1187 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1188 {
1189 	switch (size) {
1190 	case PCI_SIZE_8:
1191 		return (value >> ((offset & 3) * 8)) & 0xff;
1192 	case PCI_SIZE_16:
1193 		return (value >> ((offset & 2) * 8)) & 0xffff;
1194 	default:
1195 		return value;
1196 	}
1197 }
1198 
pci_conv_size_to_32(ulong old,ulong value,uint offset,enum pci_size_t size)1199 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1200 			  enum pci_size_t size)
1201 {
1202 	uint off_mask;
1203 	uint val_mask, shift;
1204 	ulong ldata, mask;
1205 
1206 	switch (size) {
1207 	case PCI_SIZE_8:
1208 		off_mask = 3;
1209 		val_mask = 0xff;
1210 		break;
1211 	case PCI_SIZE_16:
1212 		off_mask = 2;
1213 		val_mask = 0xffff;
1214 		break;
1215 	default:
1216 		return value;
1217 	}
1218 	shift = (offset & off_mask) * 8;
1219 	ldata = (value & val_mask) << shift;
1220 	mask = val_mask << shift;
1221 	value = (old & ~mask) | ldata;
1222 
1223 	return value;
1224 }
1225 
pci_get_dma_regions(struct udevice * dev,struct pci_region * memp,int index)1226 int pci_get_dma_regions(struct udevice *dev, struct pci_region *memp, int index)
1227 {
1228 	int pci_addr_cells, addr_cells, size_cells;
1229 	int cells_per_record;
1230 	const u32 *prop;
1231 	int len;
1232 	int i = 0;
1233 
1234 	prop = ofnode_get_property(dev_ofnode(dev), "dma-ranges", &len);
1235 	if (!prop) {
1236 		log_err("PCI: Device '%s': Cannot decode dma-ranges\n",
1237 			dev->name);
1238 		return -EINVAL;
1239 	}
1240 
1241 	pci_addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev));
1242 	addr_cells = ofnode_read_simple_addr_cells(dev_ofnode(dev->parent));
1243 	size_cells = ofnode_read_simple_size_cells(dev_ofnode(dev));
1244 
1245 	/* PCI addresses are always 3-cells */
1246 	len /= sizeof(u32);
1247 	cells_per_record = pci_addr_cells + addr_cells + size_cells;
1248 	debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
1249 	      cells_per_record);
1250 
1251 	while (len) {
1252 		memp->bus_start = fdtdec_get_number(prop + 1, 2);
1253 		prop += pci_addr_cells;
1254 		memp->phys_start = fdtdec_get_number(prop, addr_cells);
1255 		prop += addr_cells;
1256 		memp->size = fdtdec_get_number(prop, size_cells);
1257 		prop += size_cells;
1258 
1259 		if (i == index)
1260 			return 0;
1261 		i++;
1262 		len -= cells_per_record;
1263 	}
1264 
1265 	return -EINVAL;
1266 }
1267 
pci_get_regions(struct udevice * dev,struct pci_region ** iop,struct pci_region ** memp,struct pci_region ** prefp)1268 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1269 		    struct pci_region **memp, struct pci_region **prefp)
1270 {
1271 	struct udevice *bus = pci_get_controller(dev);
1272 	struct pci_controller *hose = dev_get_uclass_priv(bus);
1273 	int i;
1274 
1275 	*iop = NULL;
1276 	*memp = NULL;
1277 	*prefp = NULL;
1278 	for (i = 0; i < hose->region_count; i++) {
1279 		switch (hose->regions[i].flags) {
1280 		case PCI_REGION_IO:
1281 			if (!*iop || (*iop)->size < hose->regions[i].size)
1282 				*iop = hose->regions + i;
1283 			break;
1284 		case PCI_REGION_MEM:
1285 			if (!*memp || (*memp)->size < hose->regions[i].size)
1286 				*memp = hose->regions + i;
1287 			break;
1288 		case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1289 			if (!*prefp || (*prefp)->size < hose->regions[i].size)
1290 				*prefp = hose->regions + i;
1291 			break;
1292 		}
1293 	}
1294 
1295 	return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1296 }
1297 
dm_pci_read_bar32(const struct udevice * dev,int barnum)1298 u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
1299 {
1300 	u32 addr;
1301 	int bar;
1302 
1303 	bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1304 	dm_pci_read_config32(dev, bar, &addr);
1305 
1306 	/*
1307 	 * If we get an invalid address, return this so that comparisons with
1308 	 * FDT_ADDR_T_NONE work correctly
1309 	 */
1310 	if (addr == 0xffffffff)
1311 		return addr;
1312 	else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1313 		return addr & PCI_BASE_ADDRESS_IO_MASK;
1314 	else
1315 		return addr & PCI_BASE_ADDRESS_MEM_MASK;
1316 }
1317 
dm_pci_write_bar32(struct udevice * dev,int barnum,u32 addr)1318 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1319 {
1320 	int bar;
1321 
1322 	bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1323 	dm_pci_write_config32(dev, bar, addr);
1324 }
1325 
_dm_pci_bus_to_phys(struct udevice * ctlr,pci_addr_t bus_addr,unsigned long flags,unsigned long skip_mask,phys_addr_t * pa)1326 static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1327 			       pci_addr_t bus_addr, unsigned long flags,
1328 			       unsigned long skip_mask, phys_addr_t *pa)
1329 {
1330 	struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1331 	struct pci_region *res;
1332 	int i;
1333 
1334 	if (hose->region_count == 0) {
1335 		*pa = bus_addr;
1336 		return 0;
1337 	}
1338 
1339 	for (i = 0; i < hose->region_count; i++) {
1340 		res = &hose->regions[i];
1341 
1342 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1343 			continue;
1344 
1345 		if (res->flags & skip_mask)
1346 			continue;
1347 
1348 		if (bus_addr >= res->bus_start &&
1349 		    (bus_addr - res->bus_start) < res->size) {
1350 			*pa = (bus_addr - res->bus_start + res->phys_start);
1351 			return 0;
1352 		}
1353 	}
1354 
1355 	return 1;
1356 }
1357 
dm_pci_bus_to_phys(struct udevice * dev,pci_addr_t bus_addr,unsigned long flags)1358 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1359 			       unsigned long flags)
1360 {
1361 	phys_addr_t phys_addr = 0;
1362 	struct udevice *ctlr;
1363 	int ret;
1364 
1365 	/* The root controller has the region information */
1366 	ctlr = pci_get_controller(dev);
1367 
1368 	/*
1369 	 * if PCI_REGION_MEM is set we do a two pass search with preference
1370 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
1371 	 */
1372 	if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1373 		ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1374 					  flags, PCI_REGION_SYS_MEMORY,
1375 					  &phys_addr);
1376 		if (!ret)
1377 			return phys_addr;
1378 	}
1379 
1380 	ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1381 
1382 	if (ret)
1383 		puts("pci_hose_bus_to_phys: invalid physical address\n");
1384 
1385 	return phys_addr;
1386 }
1387 
_dm_pci_phys_to_bus(struct udevice * dev,phys_addr_t phys_addr,unsigned long flags,unsigned long skip_mask,pci_addr_t * ba)1388 int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1389 			unsigned long flags, unsigned long skip_mask,
1390 			pci_addr_t *ba)
1391 {
1392 	struct pci_region *res;
1393 	struct udevice *ctlr;
1394 	pci_addr_t bus_addr;
1395 	int i;
1396 	struct pci_controller *hose;
1397 
1398 	/* The root controller has the region information */
1399 	ctlr = pci_get_controller(dev);
1400 	hose = dev_get_uclass_priv(ctlr);
1401 
1402 	if (hose->region_count == 0) {
1403 		*ba = phys_addr;
1404 		return 0;
1405 	}
1406 
1407 	for (i = 0; i < hose->region_count; i++) {
1408 		res = &hose->regions[i];
1409 
1410 		if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1411 			continue;
1412 
1413 		if (res->flags & skip_mask)
1414 			continue;
1415 
1416 		bus_addr = phys_addr - res->phys_start + res->bus_start;
1417 
1418 		if (bus_addr >= res->bus_start &&
1419 		    (bus_addr - res->bus_start) < res->size) {
1420 			*ba = bus_addr;
1421 			return 0;
1422 		}
1423 	}
1424 
1425 	return 1;
1426 }
1427 
dm_pci_phys_to_bus(struct udevice * dev,phys_addr_t phys_addr,unsigned long flags)1428 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1429 			      unsigned long flags)
1430 {
1431 	pci_addr_t bus_addr = 0;
1432 	int ret;
1433 
1434 	/*
1435 	 * if PCI_REGION_MEM is set we do a two pass search with preference
1436 	 * on matches that don't have PCI_REGION_SYS_MEMORY set
1437 	 */
1438 	if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1439 		ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1440 					  PCI_REGION_SYS_MEMORY, &bus_addr);
1441 		if (!ret)
1442 			return bus_addr;
1443 	}
1444 
1445 	ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1446 
1447 	if (ret)
1448 		puts("pci_hose_phys_to_bus: invalid physical address\n");
1449 
1450 	return bus_addr;
1451 }
1452 
dm_pci_map_ea_virt(struct udevice * dev,int ea_off,struct pci_child_plat * pdata)1453 static phys_addr_t dm_pci_map_ea_virt(struct udevice *dev, int ea_off,
1454 				      struct pci_child_plat *pdata)
1455 {
1456 	phys_addr_t addr = 0;
1457 
1458 	/*
1459 	 * In the case of a Virtual Function device using BAR
1460 	 * base and size, add offset for VFn BAR(1, 2, 3...n)
1461 	 */
1462 	if (pdata->is_virtfn) {
1463 		size_t sz;
1464 		u32 ea_entry;
1465 
1466 		/* MaxOffset, 1st DW */
1467 		dm_pci_read_config32(dev, ea_off + 8, &ea_entry);
1468 		sz = ea_entry & PCI_EA_FIELD_MASK;
1469 		/* Fill up lower 2 bits */
1470 		sz |= (~PCI_EA_FIELD_MASK);
1471 
1472 		if (ea_entry & PCI_EA_IS_64) {
1473 			/* MaxOffset 2nd DW */
1474 			dm_pci_read_config32(dev, ea_off + 16, &ea_entry);
1475 			sz |= ((u64)ea_entry) << 32;
1476 		}
1477 
1478 		addr = (pdata->virtid - 1) * (sz + 1);
1479 	}
1480 
1481 	return addr;
1482 }
1483 
dm_pci_map_ea_bar(struct udevice * dev,int bar,int flags,int ea_off,struct pci_child_plat * pdata)1484 static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1485 			       int ea_off, struct pci_child_plat *pdata)
1486 {
1487 	int ea_cnt, i, entry_size;
1488 	int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1489 	u32 ea_entry;
1490 	phys_addr_t addr;
1491 
1492 	if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1493 		/*
1494 		 * In the case of a Virtual Function device, device is
1495 		 * Physical function, so pdata will point to required VF
1496 		 * specific data.
1497 		 */
1498 		if (pdata->is_virtfn)
1499 			bar_id += PCI_EA_BEI_VF_BAR0;
1500 	}
1501 
1502 	/* EA capability structure header */
1503 	dm_pci_read_config32(dev, ea_off, &ea_entry);
1504 	ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1505 	ea_off += PCI_EA_FIRST_ENT;
1506 
1507 	for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1508 		/* Entry header */
1509 		dm_pci_read_config32(dev, ea_off, &ea_entry);
1510 		entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1511 
1512 		if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1513 			continue;
1514 
1515 		/* Base address, 1st DW */
1516 		dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1517 		addr = ea_entry & PCI_EA_FIELD_MASK;
1518 		if (ea_entry & PCI_EA_IS_64) {
1519 			/* Base address, 2nd DW, skip over 4B MaxOffset */
1520 			dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1521 			addr |= ((u64)ea_entry) << 32;
1522 		}
1523 
1524 		if (IS_ENABLED(CONFIG_PCI_SRIOV))
1525 			addr += dm_pci_map_ea_virt(dev, ea_off, pdata);
1526 
1527 		/* size ignored for now */
1528 		return map_physmem(addr, 0, flags);
1529 	}
1530 
1531 	return 0;
1532 }
1533 
dm_pci_map_bar(struct udevice * dev,int bar,int flags)1534 void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1535 {
1536 	struct pci_child_plat *pdata = dev_get_parent_plat(dev);
1537 	struct udevice *udev = dev;
1538 	pci_addr_t pci_bus_addr;
1539 	u32 bar_response;
1540 	int ea_off;
1541 
1542 	if (IS_ENABLED(CONFIG_PCI_SRIOV)) {
1543 		/*
1544 		 * In case of Virtual Function devices, use PF udevice
1545 		 * as EA capability is defined in Physical Function
1546 		 */
1547 		if (pdata->is_virtfn)
1548 			udev = pdata->pfdev;
1549 	}
1550 
1551 	/*
1552 	 * if the function supports Enhanced Allocation use that instead of
1553 	 * BARs
1554 	 * Incase of virtual functions, pdata will help read VF BEI
1555 	 * and EA entry size.
1556 	 */
1557 	ea_off = dm_pci_find_capability(udev, PCI_CAP_ID_EA);
1558 	if (ea_off)
1559 		return dm_pci_map_ea_bar(udev, bar, flags, ea_off, pdata);
1560 
1561 	/* read BAR address */
1562 	dm_pci_read_config32(udev, bar, &bar_response);
1563 	pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1564 
1565 	/*
1566 	 * Pass "0" as the length argument to pci_bus_to_virt.  The arg
1567 	 * isn't actually used on any platform because U-Boot assumes a static
1568 	 * linear mapping.  In the future, this could read the BAR size
1569 	 * and pass that as the size if needed.
1570 	 */
1571 	return dm_pci_bus_to_virt(udev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1572 }
1573 
_dm_pci_find_next_capability(struct udevice * dev,u8 pos,int cap)1574 static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
1575 {
1576 	int ttl = PCI_FIND_CAP_TTL;
1577 	u8 id;
1578 	u16 ent;
1579 
1580 	dm_pci_read_config8(dev, pos, &pos);
1581 
1582 	while (ttl--) {
1583 		if (pos < PCI_STD_HEADER_SIZEOF)
1584 			break;
1585 		pos &= ~3;
1586 		dm_pci_read_config16(dev, pos, &ent);
1587 
1588 		id = ent & 0xff;
1589 		if (id == 0xff)
1590 			break;
1591 		if (id == cap)
1592 			return pos;
1593 		pos = (ent >> 8);
1594 	}
1595 
1596 	return 0;
1597 }
1598 
dm_pci_find_next_capability(struct udevice * dev,u8 start,int cap)1599 int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1600 {
1601 	return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1602 					    cap);
1603 }
1604 
dm_pci_find_capability(struct udevice * dev,int cap)1605 int dm_pci_find_capability(struct udevice *dev, int cap)
1606 {
1607 	u16 status;
1608 	u8 header_type;
1609 	u8 pos;
1610 
1611 	dm_pci_read_config16(dev, PCI_STATUS, &status);
1612 	if (!(status & PCI_STATUS_CAP_LIST))
1613 		return 0;
1614 
1615 	dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1616 	if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1617 		pos = PCI_CB_CAPABILITY_LIST;
1618 	else
1619 		pos = PCI_CAPABILITY_LIST;
1620 
1621 	return _dm_pci_find_next_capability(dev, pos, cap);
1622 }
1623 
dm_pci_find_next_ext_capability(struct udevice * dev,int start,int cap)1624 int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
1625 {
1626 	u32 header;
1627 	int ttl;
1628 	int pos = PCI_CFG_SPACE_SIZE;
1629 
1630 	/* minimum 8 bytes per capability */
1631 	ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1632 
1633 	if (start)
1634 		pos = start;
1635 
1636 	dm_pci_read_config32(dev, pos, &header);
1637 	/*
1638 	 * If we have no capabilities, this is indicated by cap ID,
1639 	 * cap version and next pointer all being 0.
1640 	 */
1641 	if (header == 0)
1642 		return 0;
1643 
1644 	while (ttl--) {
1645 		if (PCI_EXT_CAP_ID(header) == cap)
1646 			return pos;
1647 
1648 		pos = PCI_EXT_CAP_NEXT(header);
1649 		if (pos < PCI_CFG_SPACE_SIZE)
1650 			break;
1651 
1652 		dm_pci_read_config32(dev, pos, &header);
1653 	}
1654 
1655 	return 0;
1656 }
1657 
dm_pci_find_ext_capability(struct udevice * dev,int cap)1658 int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1659 {
1660 	return dm_pci_find_next_ext_capability(dev, 0, cap);
1661 }
1662 
dm_pci_flr(struct udevice * dev)1663 int dm_pci_flr(struct udevice *dev)
1664 {
1665 	int pcie_off;
1666 	u32 cap;
1667 
1668 	/* look for PCI Express Capability */
1669 	pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1670 	if (!pcie_off)
1671 		return -ENOENT;
1672 
1673 	/* check FLR capability */
1674 	dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1675 	if (!(cap & PCI_EXP_DEVCAP_FLR))
1676 		return -ENOENT;
1677 
1678 	dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1679 			       PCI_EXP_DEVCTL_BCR_FLR);
1680 
1681 	/* wait 100ms, per PCI spec */
1682 	mdelay(100);
1683 
1684 	return 0;
1685 }
1686 
1687 #if defined(CONFIG_PCI_SRIOV)
pci_sriov_init(struct udevice * pdev,int vf_en)1688 int pci_sriov_init(struct udevice *pdev, int vf_en)
1689 {
1690 	u16 vendor, device;
1691 	struct udevice *bus;
1692 	struct udevice *dev;
1693 	pci_dev_t bdf;
1694 	u16 ctrl;
1695 	u16 num_vfs;
1696 	u16 total_vf;
1697 	u16 vf_offset;
1698 	u16 vf_stride;
1699 	int vf, ret;
1700 	int pos;
1701 
1702 	pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1703 	if (!pos) {
1704 		debug("Error: SRIOV capability not found\n");
1705 		return -ENOENT;
1706 	}
1707 
1708 	dm_pci_read_config16(pdev, pos + PCI_SRIOV_CTRL, &ctrl);
1709 
1710 	dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1711 	if (vf_en > total_vf)
1712 		vf_en = total_vf;
1713 	dm_pci_write_config16(pdev, pos + PCI_SRIOV_NUM_VF, vf_en);
1714 
1715 	ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
1716 	dm_pci_write_config16(pdev, pos + PCI_SRIOV_CTRL, ctrl);
1717 
1718 	dm_pci_read_config16(pdev, pos + PCI_SRIOV_NUM_VF, &num_vfs);
1719 	if (num_vfs > vf_en)
1720 		num_vfs = vf_en;
1721 
1722 	dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_OFFSET, &vf_offset);
1723 	dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_STRIDE, &vf_stride);
1724 
1725 	dm_pci_read_config16(pdev, PCI_VENDOR_ID, &vendor);
1726 	dm_pci_read_config16(pdev, pos + PCI_SRIOV_VF_DID, &device);
1727 
1728 	bdf = dm_pci_get_bdf(pdev);
1729 
1730 	pci_get_bus(PCI_BUS(bdf), &bus);
1731 
1732 	if (!bus)
1733 		return -ENODEV;
1734 
1735 	bdf += PCI_BDF(0, 0, vf_offset);
1736 
1737 	for (vf = 0; vf < num_vfs; vf++) {
1738 		struct pci_child_plat *pplat;
1739 		ulong class;
1740 
1741 		pci_bus_read_config(bus, bdf, PCI_CLASS_DEVICE,
1742 				    &class, PCI_SIZE_16);
1743 
1744 		debug("%s: bus %d/%s: found VF %x:%x\n", __func__,
1745 		      dev_seq(bus), bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
1746 
1747 		/* Find this device in the device tree */
1748 		ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
1749 
1750 		if (ret == -ENODEV) {
1751 			struct pci_device_id find_id;
1752 
1753 			memset(&find_id, '\0', sizeof(find_id));
1754 			find_id.vendor = vendor;
1755 			find_id.device = device;
1756 			find_id.class = class;
1757 
1758 			ret = pci_find_and_bind_driver(bus, &find_id,
1759 						       bdf, &dev);
1760 
1761 			if (ret)
1762 				return ret;
1763 		}
1764 
1765 		/* Update the platform data */
1766 		pplat = dev_get_parent_plat(dev);
1767 		pplat->devfn = PCI_MASK_BUS(bdf);
1768 		pplat->vendor = vendor;
1769 		pplat->device = device;
1770 		pplat->class = class;
1771 		pplat->is_virtfn = true;
1772 		pplat->pfdev = pdev;
1773 		pplat->virtid = vf * vf_stride + vf_offset;
1774 
1775 		debug("%s: bus %d/%s: found VF %x:%x %x:%x class %lx id %x\n",
1776 		      __func__, dev_seq(dev), dev->name, PCI_DEV(bdf),
1777 		      PCI_FUNC(bdf), vendor, device, class, pplat->virtid);
1778 		bdf += PCI_BDF(0, 0, vf_stride);
1779 	}
1780 
1781 	return 0;
1782 }
1783 
pci_sriov_get_totalvfs(struct udevice * pdev)1784 int pci_sriov_get_totalvfs(struct udevice *pdev)
1785 {
1786 	u16 total_vf;
1787 	int pos;
1788 
1789 	pos = dm_pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1790 	if (!pos) {
1791 		debug("Error: SRIOV capability not found\n");
1792 		return -ENOENT;
1793 	}
1794 
1795 	dm_pci_read_config16(pdev, pos + PCI_SRIOV_TOTAL_VF, &total_vf);
1796 
1797 	return total_vf;
1798 }
1799 #endif /* SRIOV */
1800 
1801 UCLASS_DRIVER(pci) = {
1802 	.id		= UCLASS_PCI,
1803 	.name		= "pci",
1804 	.flags		= DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
1805 	.post_bind	= dm_scan_fdt_dev,
1806 	.pre_probe	= pci_uclass_pre_probe,
1807 	.post_probe	= pci_uclass_post_probe,
1808 	.child_post_bind = pci_uclass_child_post_bind,
1809 	.per_device_auto	= sizeof(struct pci_controller),
1810 	.per_child_plat_auto	= sizeof(struct pci_child_plat),
1811 };
1812 
1813 static const struct dm_pci_ops pci_bridge_ops = {
1814 	.read_config	= pci_bridge_read_config,
1815 	.write_config	= pci_bridge_write_config,
1816 };
1817 
1818 static const struct udevice_id pci_bridge_ids[] = {
1819 	{ .compatible = "pci-bridge" },
1820 	{ }
1821 };
1822 
1823 U_BOOT_DRIVER(pci_bridge_drv) = {
1824 	.name		= "pci_bridge_drv",
1825 	.id		= UCLASS_PCI,
1826 	.of_match	= pci_bridge_ids,
1827 	.ops		= &pci_bridge_ops,
1828 };
1829 
1830 UCLASS_DRIVER(pci_generic) = {
1831 	.id		= UCLASS_PCI_GENERIC,
1832 	.name		= "pci_generic",
1833 };
1834 
1835 static const struct udevice_id pci_generic_ids[] = {
1836 	{ .compatible = "pci-generic" },
1837 	{ }
1838 };
1839 
1840 U_BOOT_DRIVER(pci_generic_drv) = {
1841 	.name		= "pci_generic_drv",
1842 	.id		= UCLASS_PCI_GENERIC,
1843 	.of_match	= pci_generic_ids,
1844 };
1845 
pci_init(void)1846 int pci_init(void)
1847 {
1848 	struct udevice *bus;
1849 
1850 	/*
1851 	 * Enumerate all known controller devices. Enumeration has the side-
1852 	 * effect of probing them, so PCIe devices will be enumerated too.
1853 	 */
1854 	for (uclass_first_device_check(UCLASS_PCI, &bus);
1855 	     bus;
1856 	     uclass_next_device_check(&bus)) {
1857 		;
1858 	}
1859 
1860 	return 0;
1861 }
1862