pci.c 28 KB

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