pci.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* pci.c: UltraSparc PCI controller support.
  3. *
  4. * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
  5. * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
  7. *
  8. * OF tree based PCI bus probing taken from the PowerPC port
  9. * with minor modifications, see there for credits.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/sched.h>
  15. #include <linux/capability.h>
  16. #include <linux/errno.h>
  17. #include <linux/pci.h>
  18. #include <linux/msi.h>
  19. #include <linux/irq.h>
  20. #include <linux/init.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/pgtable.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/irq.h>
  27. #include <asm/prom.h>
  28. #include <asm/apb.h>
  29. #include "pci_impl.h"
  30. #include "kernel.h"
  31. /* List of all PCI controllers found in the system. */
  32. struct pci_pbm_info *pci_pbm_root = NULL;
  33. /* Each PBM found gets a unique index. */
  34. int pci_num_pbms = 0;
  35. volatile int pci_poke_in_progress;
  36. volatile int pci_poke_cpu = -1;
  37. volatile int pci_poke_faulted;
  38. static DEFINE_SPINLOCK(pci_poke_lock);
  39. void pci_config_read8(u8 *addr, u8 *ret)
  40. {
  41. unsigned long flags;
  42. u8 byte;
  43. spin_lock_irqsave(&pci_poke_lock, flags);
  44. pci_poke_cpu = smp_processor_id();
  45. pci_poke_in_progress = 1;
  46. pci_poke_faulted = 0;
  47. __asm__ __volatile__("membar #Sync\n\t"
  48. "lduba [%1] %2, %0\n\t"
  49. "membar #Sync"
  50. : "=r" (byte)
  51. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  52. : "memory");
  53. pci_poke_in_progress = 0;
  54. pci_poke_cpu = -1;
  55. if (!pci_poke_faulted)
  56. *ret = byte;
  57. spin_unlock_irqrestore(&pci_poke_lock, flags);
  58. }
  59. void pci_config_read16(u16 *addr, u16 *ret)
  60. {
  61. unsigned long flags;
  62. u16 word;
  63. spin_lock_irqsave(&pci_poke_lock, flags);
  64. pci_poke_cpu = smp_processor_id();
  65. pci_poke_in_progress = 1;
  66. pci_poke_faulted = 0;
  67. __asm__ __volatile__("membar #Sync\n\t"
  68. "lduha [%1] %2, %0\n\t"
  69. "membar #Sync"
  70. : "=r" (word)
  71. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  72. : "memory");
  73. pci_poke_in_progress = 0;
  74. pci_poke_cpu = -1;
  75. if (!pci_poke_faulted)
  76. *ret = word;
  77. spin_unlock_irqrestore(&pci_poke_lock, flags);
  78. }
  79. void pci_config_read32(u32 *addr, u32 *ret)
  80. {
  81. unsigned long flags;
  82. u32 dword;
  83. spin_lock_irqsave(&pci_poke_lock, flags);
  84. pci_poke_cpu = smp_processor_id();
  85. pci_poke_in_progress = 1;
  86. pci_poke_faulted = 0;
  87. __asm__ __volatile__("membar #Sync\n\t"
  88. "lduwa [%1] %2, %0\n\t"
  89. "membar #Sync"
  90. : "=r" (dword)
  91. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  92. : "memory");
  93. pci_poke_in_progress = 0;
  94. pci_poke_cpu = -1;
  95. if (!pci_poke_faulted)
  96. *ret = dword;
  97. spin_unlock_irqrestore(&pci_poke_lock, flags);
  98. }
  99. void pci_config_write8(u8 *addr, u8 val)
  100. {
  101. unsigned long flags;
  102. spin_lock_irqsave(&pci_poke_lock, flags);
  103. pci_poke_cpu = smp_processor_id();
  104. pci_poke_in_progress = 1;
  105. pci_poke_faulted = 0;
  106. __asm__ __volatile__("membar #Sync\n\t"
  107. "stba %0, [%1] %2\n\t"
  108. "membar #Sync"
  109. : /* no outputs */
  110. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  111. : "memory");
  112. pci_poke_in_progress = 0;
  113. pci_poke_cpu = -1;
  114. spin_unlock_irqrestore(&pci_poke_lock, flags);
  115. }
  116. void pci_config_write16(u16 *addr, u16 val)
  117. {
  118. unsigned long flags;
  119. spin_lock_irqsave(&pci_poke_lock, flags);
  120. pci_poke_cpu = smp_processor_id();
  121. pci_poke_in_progress = 1;
  122. pci_poke_faulted = 0;
  123. __asm__ __volatile__("membar #Sync\n\t"
  124. "stha %0, [%1] %2\n\t"
  125. "membar #Sync"
  126. : /* no outputs */
  127. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  128. : "memory");
  129. pci_poke_in_progress = 0;
  130. pci_poke_cpu = -1;
  131. spin_unlock_irqrestore(&pci_poke_lock, flags);
  132. }
  133. void pci_config_write32(u32 *addr, u32 val)
  134. {
  135. unsigned long flags;
  136. spin_lock_irqsave(&pci_poke_lock, flags);
  137. pci_poke_cpu = smp_processor_id();
  138. pci_poke_in_progress = 1;
  139. pci_poke_faulted = 0;
  140. __asm__ __volatile__("membar #Sync\n\t"
  141. "stwa %0, [%1] %2\n\t"
  142. "membar #Sync"
  143. : /* no outputs */
  144. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  145. : "memory");
  146. pci_poke_in_progress = 0;
  147. pci_poke_cpu = -1;
  148. spin_unlock_irqrestore(&pci_poke_lock, flags);
  149. }
  150. static int ofpci_verbose;
  151. static int __init ofpci_debug(char *str)
  152. {
  153. int val = 0;
  154. get_option(&str, &val);
  155. if (val)
  156. ofpci_verbose = 1;
  157. return 1;
  158. }
  159. __setup("ofpci_debug=", ofpci_debug);
  160. static unsigned long pci_parse_of_flags(u32 addr0)
  161. {
  162. unsigned long flags = 0;
  163. if (addr0 & 0x02000000) {
  164. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  165. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  166. if (addr0 & 0x01000000)
  167. flags |= IORESOURCE_MEM_64
  168. | PCI_BASE_ADDRESS_MEM_TYPE_64;
  169. if (addr0 & 0x40000000)
  170. flags |= IORESOURCE_PREFETCH
  171. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  172. } else if (addr0 & 0x01000000)
  173. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  174. return flags;
  175. }
  176. /* The of_device layer has translated all of the assigned-address properties
  177. * into physical address resources, we only have to figure out the register
  178. * mapping.
  179. */
  180. static void pci_parse_of_addrs(struct platform_device *op,
  181. struct device_node *node,
  182. struct pci_dev *dev)
  183. {
  184. struct resource *op_res;
  185. const u32 *addrs;
  186. int proplen;
  187. addrs = of_get_property(node, "assigned-addresses", &proplen);
  188. if (!addrs)
  189. return;
  190. if (ofpci_verbose)
  191. pci_info(dev, " parse addresses (%d bytes) @ %p\n",
  192. proplen, addrs);
  193. op_res = &op->resource[0];
  194. for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
  195. struct resource *res;
  196. unsigned long flags;
  197. int i;
  198. flags = pci_parse_of_flags(addrs[0]);
  199. if (!flags)
  200. continue;
  201. i = addrs[0] & 0xff;
  202. if (ofpci_verbose)
  203. pci_info(dev, " start: %llx, end: %llx, i: %x\n",
  204. op_res->start, op_res->end, i);
  205. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  206. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  207. } else if (i == dev->rom_base_reg) {
  208. res = &dev->resource[PCI_ROM_RESOURCE];
  209. flags |= IORESOURCE_READONLY | IORESOURCE_SIZEALIGN;
  210. } else {
  211. pci_err(dev, "bad cfg reg num 0x%x\n", i);
  212. continue;
  213. }
  214. res->start = op_res->start;
  215. res->end = op_res->end;
  216. res->flags = flags;
  217. res->name = pci_name(dev);
  218. pci_info(dev, "reg 0x%x: %pR\n", i, res);
  219. }
  220. }
  221. static void pci_init_dev_archdata(struct dev_archdata *sd, void *iommu,
  222. void *stc, void *host_controller,
  223. struct platform_device *op,
  224. int numa_node)
  225. {
  226. sd->iommu = iommu;
  227. sd->stc = stc;
  228. sd->host_controller = host_controller;
  229. sd->op = op;
  230. sd->numa_node = numa_node;
  231. }
  232. static struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  233. struct device_node *node,
  234. struct pci_bus *bus, int devfn)
  235. {
  236. struct dev_archdata *sd;
  237. struct platform_device *op;
  238. struct pci_dev *dev;
  239. u32 class;
  240. dev = pci_alloc_dev(bus);
  241. if (!dev)
  242. return NULL;
  243. op = of_find_device_by_node(node);
  244. sd = &dev->dev.archdata;
  245. pci_init_dev_archdata(sd, pbm->iommu, &pbm->stc, pbm, op,
  246. pbm->numa_node);
  247. sd = &op->dev.archdata;
  248. sd->iommu = pbm->iommu;
  249. sd->stc = &pbm->stc;
  250. sd->numa_node = pbm->numa_node;
  251. if (of_node_name_eq(node, "ebus"))
  252. of_propagate_archdata(op);
  253. if (ofpci_verbose)
  254. pci_info(bus," create device, devfn: %x, type: %s\n",
  255. devfn, of_node_get_device_type(node));
  256. dev->sysdata = node;
  257. dev->dev.parent = bus->bridge;
  258. dev->dev.bus = &pci_bus_type;
  259. dev->dev.of_node = of_node_get(node);
  260. dev->devfn = devfn;
  261. dev->multifunction = 0; /* maybe a lie? */
  262. set_pcie_port_type(dev);
  263. pci_dev_assign_slot(dev);
  264. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  265. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  266. dev->subsystem_vendor =
  267. of_getintprop_default(node, "subsystem-vendor-id", 0);
  268. dev->subsystem_device =
  269. of_getintprop_default(node, "subsystem-id", 0);
  270. dev->cfg_size = pci_cfg_space_size(dev);
  271. /* We can't actually use the firmware value, we have
  272. * to read what is in the register right now. One
  273. * reason is that in the case of IDE interfaces the
  274. * firmware can sample the value before the IDE
  275. * interface is programmed into native mode.
  276. */
  277. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  278. dev->class = class >> 8;
  279. dev->revision = class & 0xff;
  280. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  281. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  282. /* I have seen IDE devices which will not respond to
  283. * the bmdma simplex check reads if bus mastering is
  284. * disabled.
  285. */
  286. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
  287. pci_set_master(dev);
  288. dev->current_state = PCI_UNKNOWN; /* unknown power state */
  289. dev->error_state = pci_channel_io_normal;
  290. dev->dma_mask = 0xffffffff;
  291. if (of_node_name_eq(node, "pci")) {
  292. /* a PCI-PCI bridge */
  293. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  294. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  295. } else if (of_node_is_type(node, "cardbus")) {
  296. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  297. } else {
  298. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  299. dev->rom_base_reg = PCI_ROM_ADDRESS;
  300. dev->irq = sd->op->archdata.irqs[0];
  301. if (dev->irq == 0xffffffff)
  302. dev->irq = PCI_IRQ_NONE;
  303. }
  304. pci_info(dev, "[%04x:%04x] type %02x class %#08x\n",
  305. dev->vendor, dev->device, dev->hdr_type, dev->class);
  306. pci_parse_of_addrs(sd->op, node, dev);
  307. if (ofpci_verbose)
  308. pci_info(dev, " adding to system ...\n");
  309. pci_device_add(dev, bus);
  310. return dev;
  311. }
  312. static void apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
  313. {
  314. u32 idx, first, last;
  315. first = 8;
  316. last = 0;
  317. for (idx = 0; idx < 8; idx++) {
  318. if ((map & (1 << idx)) != 0) {
  319. if (first > idx)
  320. first = idx;
  321. if (last < idx)
  322. last = idx;
  323. }
  324. }
  325. *first_p = first;
  326. *last_p = last;
  327. }
  328. /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  329. * a proper 'ranges' property.
  330. */
  331. static void apb_fake_ranges(struct pci_dev *dev,
  332. struct pci_bus *bus,
  333. struct pci_pbm_info *pbm)
  334. {
  335. struct pci_bus_region region;
  336. struct resource *res;
  337. u32 first, last;
  338. u8 map;
  339. pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
  340. apb_calc_first_last(map, &first, &last);
  341. res = bus->resource[0];
  342. res->flags = IORESOURCE_IO;
  343. region.start = (first << 21);
  344. region.end = (last << 21) + ((1 << 21) - 1);
  345. pcibios_bus_to_resource(dev->bus, res, &region);
  346. pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
  347. apb_calc_first_last(map, &first, &last);
  348. res = bus->resource[1];
  349. res->flags = IORESOURCE_MEM;
  350. region.start = (first << 29);
  351. region.end = (last << 29) + ((1 << 29) - 1);
  352. pcibios_bus_to_resource(dev->bus, res, &region);
  353. }
  354. static void pci_of_scan_bus(struct pci_pbm_info *pbm,
  355. struct device_node *node,
  356. struct pci_bus *bus);
  357. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  358. static void of_scan_pci_bridge(struct pci_pbm_info *pbm,
  359. struct device_node *node,
  360. struct pci_dev *dev)
  361. {
  362. struct pci_bus *bus;
  363. const u32 *busrange, *ranges;
  364. int len, i, simba;
  365. struct pci_bus_region region;
  366. struct resource *res;
  367. unsigned int flags;
  368. u64 size;
  369. if (ofpci_verbose)
  370. pci_info(dev, "of_scan_pci_bridge(%pOF)\n", node);
  371. /* parse bus-range property */
  372. busrange = of_get_property(node, "bus-range", &len);
  373. if (busrange == NULL || len != 8) {
  374. pci_info(dev, "Can't get bus-range for PCI-PCI bridge %pOF\n",
  375. node);
  376. return;
  377. }
  378. if (ofpci_verbose)
  379. pci_info(dev, " Bridge bus range [%u --> %u]\n",
  380. busrange[0], busrange[1]);
  381. ranges = of_get_property(node, "ranges", &len);
  382. simba = 0;
  383. if (ranges == NULL) {
  384. const char *model = of_get_property(node, "model", NULL);
  385. if (model && !strcmp(model, "SUNW,simba"))
  386. simba = 1;
  387. }
  388. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  389. if (!bus) {
  390. pci_err(dev, "Failed to create pci bus for %pOF\n",
  391. node);
  392. return;
  393. }
  394. bus->primary = dev->bus->number;
  395. pci_bus_insert_busn_res(bus, busrange[0], busrange[1]);
  396. bus->bridge_ctl = 0;
  397. if (ofpci_verbose)
  398. pci_info(dev, " Bridge ranges[%p] simba[%d]\n",
  399. ranges, simba);
  400. /* parse ranges property, or cook one up by hand for Simba */
  401. /* PCI #address-cells == 3 and #size-cells == 2 always */
  402. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  403. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  404. res->flags = 0;
  405. bus->resource[i] = res;
  406. ++res;
  407. }
  408. if (simba) {
  409. apb_fake_ranges(dev, bus, pbm);
  410. goto after_ranges;
  411. } else if (ranges == NULL) {
  412. pci_read_bridge_bases(bus);
  413. goto after_ranges;
  414. }
  415. i = 1;
  416. for (; len >= 32; len -= 32, ranges += 8) {
  417. u64 start;
  418. if (ofpci_verbose)
  419. pci_info(dev, " RAW Range[%08x:%08x:%08x:%08x:%08x:%08x:"
  420. "%08x:%08x]\n",
  421. ranges[0], ranges[1], ranges[2], ranges[3],
  422. ranges[4], ranges[5], ranges[6], ranges[7]);
  423. flags = pci_parse_of_flags(ranges[0]);
  424. size = GET_64BIT(ranges, 6);
  425. if (flags == 0 || size == 0)
  426. continue;
  427. /* On PCI-Express systems, PCI bridges that have no devices downstream
  428. * have a bogus size value where the first 32-bit cell is 0xffffffff.
  429. * This results in a bogus range where start + size overflows.
  430. *
  431. * Just skip these otherwise the kernel will complain when the resource
  432. * tries to be claimed.
  433. */
  434. if (size >> 32 == 0xffffffff)
  435. continue;
  436. if (flags & IORESOURCE_IO) {
  437. res = bus->resource[0];
  438. if (res->flags) {
  439. pci_err(dev, "ignoring extra I/O range"
  440. " for bridge %pOF\n", node);
  441. continue;
  442. }
  443. } else {
  444. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  445. pci_err(dev, "too many memory ranges"
  446. " for bridge %pOF\n", node);
  447. continue;
  448. }
  449. res = bus->resource[i];
  450. ++i;
  451. }
  452. res->flags = flags;
  453. region.start = start = GET_64BIT(ranges, 1);
  454. region.end = region.start + size - 1;
  455. if (ofpci_verbose)
  456. pci_info(dev, " Using flags[%08x] start[%016llx] size[%016llx]\n",
  457. flags, start, size);
  458. pcibios_bus_to_resource(dev->bus, res, &region);
  459. }
  460. after_ranges:
  461. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  462. bus->number);
  463. if (ofpci_verbose)
  464. pci_info(dev, " bus name: %s\n", bus->name);
  465. pci_of_scan_bus(pbm, node, bus);
  466. }
  467. static void pci_of_scan_bus(struct pci_pbm_info *pbm,
  468. struct device_node *node,
  469. struct pci_bus *bus)
  470. {
  471. struct device_node *child;
  472. const u32 *reg;
  473. int reglen, devfn, prev_devfn;
  474. struct pci_dev *dev;
  475. if (ofpci_verbose)
  476. pci_info(bus, "scan_bus[%pOF] bus no %d\n",
  477. node, bus->number);
  478. prev_devfn = -1;
  479. for_each_child_of_node(node, child) {
  480. if (ofpci_verbose)
  481. pci_info(bus, " * %pOF\n", child);
  482. reg = of_get_property(child, "reg", &reglen);
  483. if (reg == NULL || reglen < 20)
  484. continue;
  485. devfn = (reg[0] >> 8) & 0xff;
  486. /* This is a workaround for some device trees
  487. * which list PCI devices twice. On the V100
  488. * for example, device number 3 is listed twice.
  489. * Once as "pm" and once again as "lomp".
  490. */
  491. if (devfn == prev_devfn)
  492. continue;
  493. prev_devfn = devfn;
  494. /* create a new pci_dev for this device */
  495. dev = of_create_pci_dev(pbm, child, bus, devfn);
  496. if (!dev)
  497. continue;
  498. if (ofpci_verbose)
  499. pci_info(dev, "dev header type: %x\n", dev->hdr_type);
  500. if (pci_is_bridge(dev))
  501. of_scan_pci_bridge(pbm, child, dev);
  502. }
  503. }
  504. static ssize_t
  505. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  506. {
  507. struct pci_dev *pdev;
  508. struct device_node *dp;
  509. pdev = to_pci_dev(dev);
  510. dp = pdev->dev.of_node;
  511. return scnprintf(buf, PAGE_SIZE, "%pOF\n", dp);
  512. }
  513. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  514. static void pci_bus_register_of_sysfs(struct pci_bus *bus)
  515. {
  516. struct pci_dev *dev;
  517. struct pci_bus *child_bus;
  518. int err;
  519. list_for_each_entry(dev, &bus->devices, bus_list) {
  520. /* we don't really care if we can create this file or
  521. * not, but we need to assign the result of the call
  522. * or the world will fall under alien invasion and
  523. * everybody will be frozen on a spaceship ready to be
  524. * eaten on alpha centauri by some green and jelly
  525. * humanoid.
  526. */
  527. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  528. (void) err;
  529. }
  530. list_for_each_entry(child_bus, &bus->children, node)
  531. pci_bus_register_of_sysfs(child_bus);
  532. }
  533. static void pci_claim_legacy_resources(struct pci_dev *dev)
  534. {
  535. struct pci_bus_region region;
  536. struct resource *p, *root, *conflict;
  537. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  538. return;
  539. p = kzalloc(sizeof(*p), GFP_KERNEL);
  540. if (!p)
  541. return;
  542. p->name = "Video RAM area";
  543. p->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  544. region.start = 0xa0000UL;
  545. region.end = region.start + 0x1ffffUL;
  546. pcibios_bus_to_resource(dev->bus, p, &region);
  547. root = pci_find_parent_resource(dev, p);
  548. if (!root) {
  549. pci_info(dev, "can't claim VGA legacy %pR: no compatible bridge window\n", p);
  550. goto err;
  551. }
  552. conflict = request_resource_conflict(root, p);
  553. if (conflict) {
  554. pci_info(dev, "can't claim VGA legacy %pR: address conflict with %s %pR\n",
  555. p, conflict->name, conflict);
  556. goto err;
  557. }
  558. pci_info(dev, "VGA legacy framebuffer %pR\n", p);
  559. return;
  560. err:
  561. kfree(p);
  562. }
  563. static void pci_claim_bus_resources(struct pci_bus *bus)
  564. {
  565. struct pci_bus *child_bus;
  566. struct pci_dev *dev;
  567. list_for_each_entry(dev, &bus->devices, bus_list) {
  568. struct resource *r;
  569. int i;
  570. pci_dev_for_each_resource(dev, r, i) {
  571. if (r->parent || !r->start || !r->flags)
  572. continue;
  573. if (ofpci_verbose)
  574. pci_info(dev, "Claiming Resource %d: %pR\n",
  575. i, r);
  576. pci_claim_resource(dev, i);
  577. }
  578. pci_claim_legacy_resources(dev);
  579. }
  580. list_for_each_entry(child_bus, &bus->children, node)
  581. pci_claim_bus_resources(child_bus);
  582. }
  583. struct pci_bus *pci_scan_one_pbm(struct pci_pbm_info *pbm,
  584. struct device *parent)
  585. {
  586. LIST_HEAD(resources);
  587. struct device_node *node = pbm->op->dev.of_node;
  588. struct pci_bus *bus;
  589. printk("PCI: Scanning PBM %pOF\n", node);
  590. pci_add_resource_offset(&resources, &pbm->io_space,
  591. pbm->io_offset);
  592. pci_add_resource_offset(&resources, &pbm->mem_space,
  593. pbm->mem_offset);
  594. if (pbm->mem64_space.flags)
  595. pci_add_resource_offset(&resources, &pbm->mem64_space,
  596. pbm->mem64_offset);
  597. pbm->busn.start = pbm->pci_first_busno;
  598. pbm->busn.end = pbm->pci_last_busno;
  599. pbm->busn.flags = IORESOURCE_BUS;
  600. pci_add_resource(&resources, &pbm->busn);
  601. bus = pci_create_root_bus(parent, pbm->pci_first_busno, pbm->pci_ops,
  602. pbm, &resources);
  603. if (!bus) {
  604. printk(KERN_ERR "Failed to create bus for %pOF\n", node);
  605. pci_free_resource_list(&resources);
  606. return NULL;
  607. }
  608. pci_of_scan_bus(pbm, node, bus);
  609. pci_bus_register_of_sysfs(bus);
  610. pci_claim_bus_resources(bus);
  611. pci_bus_add_devices(bus);
  612. return bus;
  613. }
  614. int pcibios_enable_device(struct pci_dev *dev, int mask)
  615. {
  616. struct resource *res;
  617. u16 cmd, oldcmd;
  618. int i;
  619. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  620. oldcmd = cmd;
  621. pci_dev_for_each_resource(dev, res, i) {
  622. /* Only set up the requested stuff */
  623. if (!(mask & (1<<i)))
  624. continue;
  625. if (res->flags & IORESOURCE_IO)
  626. cmd |= PCI_COMMAND_IO;
  627. if (res->flags & IORESOURCE_MEM)
  628. cmd |= PCI_COMMAND_MEMORY;
  629. }
  630. if (cmd != oldcmd) {
  631. pci_info(dev, "enabling device (%04x -> %04x)\n", oldcmd, cmd);
  632. pci_write_config_word(dev, PCI_COMMAND, cmd);
  633. }
  634. return 0;
  635. }
  636. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  637. int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
  638. {
  639. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  640. resource_size_t ioaddr = pci_resource_start(pdev, bar);
  641. if (!pbm)
  642. return -EINVAL;
  643. vma->vm_pgoff += (ioaddr + pbm->io_space.start) >> PAGE_SHIFT;
  644. return 0;
  645. }
  646. #ifdef CONFIG_NUMA
  647. int pcibus_to_node(struct pci_bus *pbus)
  648. {
  649. struct pci_pbm_info *pbm = pbus->sysdata;
  650. return pbm->numa_node;
  651. }
  652. EXPORT_SYMBOL(pcibus_to_node);
  653. #endif
  654. /* Return the domain number for this pci bus */
  655. int pci_domain_nr(struct pci_bus *pbus)
  656. {
  657. struct pci_pbm_info *pbm = pbus->sysdata;
  658. int ret;
  659. if (!pbm) {
  660. ret = -ENXIO;
  661. } else {
  662. ret = pbm->index;
  663. }
  664. return ret;
  665. }
  666. EXPORT_SYMBOL(pci_domain_nr);
  667. #ifdef CONFIG_PCI_MSI
  668. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  669. {
  670. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  671. unsigned int irq;
  672. if (!pbm->setup_msi_irq)
  673. return -EINVAL;
  674. return pbm->setup_msi_irq(&irq, pdev, desc);
  675. }
  676. void arch_teardown_msi_irq(unsigned int irq)
  677. {
  678. struct msi_desc *entry = irq_get_msi_desc(irq);
  679. struct pci_dev *pdev = msi_desc_to_pci_dev(entry);
  680. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  681. if (pbm->teardown_msi_irq)
  682. pbm->teardown_msi_irq(irq, pdev);
  683. }
  684. #endif /* !(CONFIG_PCI_MSI) */
  685. /* ALI sound chips generate 31-bits of DMA, a special register
  686. * determines what bit 31 is emitted as.
  687. */
  688. int ali_sound_dma_hack(struct device *dev, u64 device_mask)
  689. {
  690. struct iommu *iommu = dev->archdata.iommu;
  691. struct pci_dev *ali_isa_bridge;
  692. u8 val;
  693. if (!dev_is_pci(dev))
  694. return 0;
  695. if (to_pci_dev(dev)->vendor != PCI_VENDOR_ID_AL ||
  696. to_pci_dev(dev)->device != PCI_DEVICE_ID_AL_M5451 ||
  697. device_mask != 0x7fffffff)
  698. return 0;
  699. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  700. PCI_DEVICE_ID_AL_M1533,
  701. NULL);
  702. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  703. if (iommu->dma_addr_mask & 0x80000000)
  704. val |= 0x01;
  705. else
  706. val &= ~0x01;
  707. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  708. pci_dev_put(ali_isa_bridge);
  709. return 1;
  710. }
  711. void pci_resource_to_user(const struct pci_dev *pdev, int bar,
  712. const struct resource *rp, resource_size_t *start,
  713. resource_size_t *end)
  714. {
  715. struct pci_bus_region region;
  716. /*
  717. * "User" addresses are shown in /sys/devices/pci.../.../resource
  718. * and /proc/bus/pci/devices and used as mmap offsets for
  719. * /proc/bus/pci/BB/DD.F files (see proc_bus_pci_mmap()).
  720. *
  721. * On sparc, these are PCI bus addresses, i.e., raw BAR values.
  722. */
  723. pcibios_resource_to_bus(pdev->bus, &region, (struct resource *) rp);
  724. *start = region.start;
  725. *end = region.end;
  726. }
  727. void pcibios_set_master(struct pci_dev *dev)
  728. {
  729. /* No special bus mastering setup handling */
  730. }
  731. #ifdef CONFIG_PCI_IOV
  732. int pcibios_device_add(struct pci_dev *dev)
  733. {
  734. struct pci_dev *pdev;
  735. /* Add sriov arch specific initialization here.
  736. * Copy dev_archdata from PF to VF
  737. */
  738. if (dev->is_virtfn) {
  739. struct dev_archdata *psd;
  740. pdev = dev->physfn;
  741. psd = &pdev->dev.archdata;
  742. pci_init_dev_archdata(&dev->dev.archdata, psd->iommu,
  743. psd->stc, psd->host_controller, NULL,
  744. psd->numa_node);
  745. }
  746. return 0;
  747. }
  748. #endif /* CONFIG_PCI_IOV */
  749. static int __init pcibios_init(void)
  750. {
  751. pci_dfl_cache_line_size = 64 >> 2;
  752. return 0;
  753. }
  754. subsys_initcall(pcibios_init);
  755. #ifdef CONFIG_SYSFS
  756. #define SLOT_NAME_SIZE 11 /* Max decimal digits + null in u32 */
  757. static void pcie_bus_slot_names(struct pci_bus *pbus)
  758. {
  759. struct pci_dev *pdev;
  760. struct pci_bus *bus;
  761. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  762. char name[SLOT_NAME_SIZE];
  763. struct pci_slot *pci_slot;
  764. const u32 *slot_num;
  765. int len;
  766. slot_num = of_get_property(pdev->dev.of_node,
  767. "physical-slot#", &len);
  768. if (slot_num == NULL || len != 4)
  769. continue;
  770. snprintf(name, sizeof(name), "%u", slot_num[0]);
  771. pci_slot = pci_create_slot(pbus, slot_num[0], name, NULL);
  772. if (IS_ERR(pci_slot))
  773. pr_err("PCI: pci_create_slot returned %ld.\n",
  774. PTR_ERR(pci_slot));
  775. }
  776. list_for_each_entry(bus, &pbus->children, node)
  777. pcie_bus_slot_names(bus);
  778. }
  779. static void pci_bus_slot_names(struct device_node *node, struct pci_bus *bus)
  780. {
  781. const struct pci_slot_names {
  782. u32 slot_mask;
  783. char names[0];
  784. } *prop;
  785. const char *sp;
  786. int len, i;
  787. u32 mask;
  788. prop = of_get_property(node, "slot-names", &len);
  789. if (!prop)
  790. return;
  791. mask = prop->slot_mask;
  792. sp = prop->names;
  793. if (ofpci_verbose)
  794. pci_info(bus, "Making slots for [%pOF] mask[0x%02x]\n",
  795. node, mask);
  796. i = 0;
  797. while (mask) {
  798. struct pci_slot *pci_slot;
  799. u32 this_bit = 1 << i;
  800. if (!(mask & this_bit)) {
  801. i++;
  802. continue;
  803. }
  804. if (ofpci_verbose)
  805. pci_info(bus, "Making slot [%s]\n", sp);
  806. pci_slot = pci_create_slot(bus, i, sp, NULL);
  807. if (IS_ERR(pci_slot))
  808. pci_err(bus, "pci_create_slot returned %ld\n",
  809. PTR_ERR(pci_slot));
  810. sp += strlen(sp) + 1;
  811. mask &= ~this_bit;
  812. i++;
  813. }
  814. }
  815. static int __init of_pci_slot_init(void)
  816. {
  817. struct pci_bus *pbus = NULL;
  818. while ((pbus = pci_find_next_bus(pbus)) != NULL) {
  819. struct device_node *node;
  820. struct pci_dev *pdev;
  821. pdev = list_first_entry(&pbus->devices, struct pci_dev,
  822. bus_list);
  823. if (pdev && pci_is_pcie(pdev)) {
  824. pcie_bus_slot_names(pbus);
  825. } else {
  826. if (pbus->self) {
  827. /* PCI->PCI bridge */
  828. node = pbus->self->dev.of_node;
  829. } else {
  830. struct pci_pbm_info *pbm = pbus->sysdata;
  831. /* Host PCI controller */
  832. node = pbm->op->dev.of_node;
  833. }
  834. pci_bus_slot_names(node, pbus);
  835. }
  836. }
  837. return 0;
  838. }
  839. device_initcall(of_pci_slot_init);
  840. #endif