pci.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. pci_info(dev, " 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. pci_info(dev, " 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. pci_err(dev, "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. pci_info(dev, "reg 0x%x: %pR\n", i, res);
  218. }
  219. }
  220. static void pci_init_dev_archdata(struct dev_archdata *sd, void *iommu,
  221. void *stc, void *host_controller,
  222. struct platform_device *op,
  223. int numa_node)
  224. {
  225. sd->iommu = iommu;
  226. sd->stc = stc;
  227. sd->host_controller = host_controller;
  228. sd->op = op;
  229. sd->numa_node = numa_node;
  230. }
  231. static struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  232. struct device_node *node,
  233. struct pci_bus *bus, int devfn)
  234. {
  235. struct dev_archdata *sd;
  236. struct platform_device *op;
  237. struct pci_dev *dev;
  238. const char *type;
  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 (!strcmp(node->name, "ebus"))
  252. of_propagate_archdata(op);
  253. type = of_get_property(node, "device_type", NULL);
  254. if (type == NULL)
  255. type = "";
  256. if (ofpci_verbose)
  257. pci_info(bus," create device, devfn: %x, type: %s\n",
  258. devfn, type);
  259. dev->sysdata = node;
  260. dev->dev.parent = bus->bridge;
  261. dev->dev.bus = &pci_bus_type;
  262. dev->dev.of_node = of_node_get(node);
  263. dev->devfn = devfn;
  264. dev->multifunction = 0; /* maybe a lie? */
  265. set_pcie_port_type(dev);
  266. pci_dev_assign_slot(dev);
  267. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  268. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  269. dev->subsystem_vendor =
  270. of_getintprop_default(node, "subsystem-vendor-id", 0);
  271. dev->subsystem_device =
  272. of_getintprop_default(node, "subsystem-id", 0);
  273. dev->cfg_size = pci_cfg_space_size(dev);
  274. /* We can't actually use the firmware value, we have
  275. * to read what is in the register right now. One
  276. * reason is that in the case of IDE interfaces the
  277. * firmware can sample the value before the the IDE
  278. * interface is programmed into native mode.
  279. */
  280. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  281. dev->class = class >> 8;
  282. dev->revision = class & 0xff;
  283. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  284. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  285. /* I have seen IDE devices which will not respond to
  286. * the bmdma simplex check reads if bus mastering is
  287. * disabled.
  288. */
  289. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
  290. pci_set_master(dev);
  291. dev->current_state = PCI_UNKNOWN; /* unknown power state */
  292. dev->error_state = pci_channel_io_normal;
  293. dev->dma_mask = 0xffffffff;
  294. if (!strcmp(node->name, "pci")) {
  295. /* a PCI-PCI bridge */
  296. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  297. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  298. } else if (!strcmp(type, "cardbus")) {
  299. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  300. } else {
  301. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  302. dev->rom_base_reg = PCI_ROM_ADDRESS;
  303. dev->irq = sd->op->archdata.irqs[0];
  304. if (dev->irq == 0xffffffff)
  305. dev->irq = PCI_IRQ_NONE;
  306. }
  307. pci_info(dev, "[%04x:%04x] type %02x class %#08x\n",
  308. dev->vendor, dev->device, dev->hdr_type, dev->class);
  309. pci_parse_of_addrs(sd->op, node, dev);
  310. if (ofpci_verbose)
  311. pci_info(dev, " 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. pci_info(dev, "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. pci_info(dev, "Can't get bus-range for PCI-PCI bridge %s\n",
  378. node->full_name);
  379. return;
  380. }
  381. if (ofpci_verbose)
  382. pci_info(dev, " 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. pci_err(dev, "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. pci_info(dev, " 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. pci_info(dev, " 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. pci_err(dev, "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. pci_err(dev, "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. pci_info(dev, " 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. pci_info(dev, " 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. pci_info(bus, "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. pci_info(bus, " * %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. pci_info(dev, "dev header type: %x\n", dev->hdr_type);
  504. if (pci_is_bridge(dev))
  505. of_scan_pci_bridge(pbm, child, dev);
  506. }
  507. }
  508. static ssize_t
  509. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  510. {
  511. struct pci_dev *pdev;
  512. struct device_node *dp;
  513. pdev = to_pci_dev(dev);
  514. dp = pdev->dev.of_node;
  515. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  516. }
  517. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  518. static void pci_bus_register_of_sysfs(struct pci_bus *bus)
  519. {
  520. struct pci_dev *dev;
  521. struct pci_bus *child_bus;
  522. int err;
  523. list_for_each_entry(dev, &bus->devices, bus_list) {
  524. /* we don't really care if we can create this file or
  525. * not, but we need to assign the result of the call
  526. * or the world will fall under alien invasion and
  527. * everybody will be frozen on a spaceship ready to be
  528. * eaten on alpha centauri by some green and jelly
  529. * humanoid.
  530. */
  531. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  532. (void) err;
  533. }
  534. list_for_each_entry(child_bus, &bus->children, node)
  535. pci_bus_register_of_sysfs(child_bus);
  536. }
  537. static void pci_claim_legacy_resources(struct pci_dev *dev)
  538. {
  539. struct pci_bus_region region;
  540. struct resource *p, *root, *conflict;
  541. if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
  542. return;
  543. p = kzalloc(sizeof(*p), GFP_KERNEL);
  544. if (!p)
  545. return;
  546. p->name = "Video RAM area";
  547. p->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  548. region.start = 0xa0000UL;
  549. region.end = region.start + 0x1ffffUL;
  550. pcibios_bus_to_resource(dev->bus, p, &region);
  551. root = pci_find_parent_resource(dev, p);
  552. if (!root) {
  553. pci_info(dev, "can't claim VGA legacy %pR: no compatible bridge window\n", p);
  554. goto err;
  555. }
  556. conflict = request_resource_conflict(root, p);
  557. if (conflict) {
  558. pci_info(dev, "can't claim VGA legacy %pR: address conflict with %s %pR\n",
  559. p, conflict->name, conflict);
  560. goto err;
  561. }
  562. pci_info(dev, "VGA legacy framebuffer %pR\n", p);
  563. return;
  564. err:
  565. kfree(p);
  566. }
  567. static void pci_claim_bus_resources(struct pci_bus *bus)
  568. {
  569. struct pci_bus *child_bus;
  570. struct pci_dev *dev;
  571. list_for_each_entry(dev, &bus->devices, bus_list) {
  572. int i;
  573. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  574. struct resource *r = &dev->resource[i];
  575. if (r->parent || !r->start || !r->flags)
  576. continue;
  577. if (ofpci_verbose)
  578. pci_info(dev, "Claiming Resource %d: %pR\n",
  579. i, r);
  580. pci_claim_resource(dev, i);
  581. }
  582. pci_claim_legacy_resources(dev);
  583. }
  584. list_for_each_entry(child_bus, &bus->children, node)
  585. pci_claim_bus_resources(child_bus);
  586. }
  587. struct pci_bus *pci_scan_one_pbm(struct pci_pbm_info *pbm,
  588. struct device *parent)
  589. {
  590. LIST_HEAD(resources);
  591. struct device_node *node = pbm->op->dev.of_node;
  592. struct pci_bus *bus;
  593. printk("PCI: Scanning PBM %s\n", node->full_name);
  594. pci_add_resource_offset(&resources, &pbm->io_space,
  595. pbm->io_offset);
  596. pci_add_resource_offset(&resources, &pbm->mem_space,
  597. pbm->mem_offset);
  598. if (pbm->mem64_space.flags)
  599. pci_add_resource_offset(&resources, &pbm->mem64_space,
  600. pbm->mem64_offset);
  601. pbm->busn.start = pbm->pci_first_busno;
  602. pbm->busn.end = pbm->pci_last_busno;
  603. pbm->busn.flags = IORESOURCE_BUS;
  604. pci_add_resource(&resources, &pbm->busn);
  605. bus = pci_create_root_bus(parent, pbm->pci_first_busno, pbm->pci_ops,
  606. pbm, &resources);
  607. if (!bus) {
  608. printk(KERN_ERR "Failed to create bus for %s\n",
  609. node->full_name);
  610. pci_free_resource_list(&resources);
  611. return NULL;
  612. }
  613. pci_of_scan_bus(pbm, node, bus);
  614. pci_bus_register_of_sysfs(bus);
  615. pci_claim_bus_resources(bus);
  616. pci_bus_add_devices(bus);
  617. return bus;
  618. }
  619. int pcibios_enable_device(struct pci_dev *dev, int mask)
  620. {
  621. u16 cmd, oldcmd;
  622. int i;
  623. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  624. oldcmd = cmd;
  625. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  626. struct resource *res = &dev->resource[i];
  627. /* Only set up the requested stuff */
  628. if (!(mask & (1<<i)))
  629. continue;
  630. if (res->flags & IORESOURCE_IO)
  631. cmd |= PCI_COMMAND_IO;
  632. if (res->flags & IORESOURCE_MEM)
  633. cmd |= PCI_COMMAND_MEMORY;
  634. }
  635. if (cmd != oldcmd) {
  636. pci_info(dev, "enabling device (%04x -> %04x)\n", oldcmd, cmd);
  637. pci_write_config_word(dev, PCI_COMMAND, cmd);
  638. }
  639. return 0;
  640. }
  641. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  642. /* If the user uses a host-bridge as the PCI device, he may use
  643. * this to perform a raw mmap() of the I/O or MEM space behind
  644. * that controller.
  645. *
  646. * This can be useful for execution of x86 PCI bios initialization code
  647. * on a PCI card, like the xfree86 int10 stuff does.
  648. */
  649. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  650. enum pci_mmap_state mmap_state)
  651. {
  652. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  653. unsigned long space_size, user_offset, user_size;
  654. if (mmap_state == pci_mmap_io) {
  655. space_size = resource_size(&pbm->io_space);
  656. } else {
  657. space_size = resource_size(&pbm->mem_space);
  658. }
  659. /* Make sure the request is in range. */
  660. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  661. user_size = vma->vm_end - vma->vm_start;
  662. if (user_offset >= space_size ||
  663. (user_offset + user_size) > space_size)
  664. return -EINVAL;
  665. if (mmap_state == pci_mmap_io) {
  666. vma->vm_pgoff = (pbm->io_space.start +
  667. user_offset) >> PAGE_SHIFT;
  668. } else {
  669. vma->vm_pgoff = (pbm->mem_space.start +
  670. user_offset) >> PAGE_SHIFT;
  671. }
  672. return 0;
  673. }
  674. /* Adjust vm_pgoff of VMA such that it is the physical page offset
  675. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  676. *
  677. * Basically, the user finds the base address for his device which he wishes
  678. * to mmap. They read the 32-bit value from the config space base register,
  679. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  680. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  681. *
  682. * Returns negative error code on failure, zero on success.
  683. */
  684. static int __pci_mmap_make_offset(struct pci_dev *pdev,
  685. struct vm_area_struct *vma,
  686. enum pci_mmap_state mmap_state)
  687. {
  688. unsigned long user_paddr, user_size;
  689. int i, err;
  690. /* First compute the physical address in vma->vm_pgoff,
  691. * making sure the user offset is within range in the
  692. * appropriate PCI space.
  693. */
  694. err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
  695. if (err)
  696. return err;
  697. /* If this is a mapping on a host bridge, any address
  698. * is OK.
  699. */
  700. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  701. return err;
  702. /* Otherwise make sure it's in the range for one of the
  703. * device's resources.
  704. */
  705. user_paddr = vma->vm_pgoff << PAGE_SHIFT;
  706. user_size = vma->vm_end - vma->vm_start;
  707. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  708. struct resource *rp = &pdev->resource[i];
  709. resource_size_t aligned_end;
  710. /* Active? */
  711. if (!rp->flags)
  712. continue;
  713. /* Same type? */
  714. if (i == PCI_ROM_RESOURCE) {
  715. if (mmap_state != pci_mmap_mem)
  716. continue;
  717. } else {
  718. if ((mmap_state == pci_mmap_io &&
  719. (rp->flags & IORESOURCE_IO) == 0) ||
  720. (mmap_state == pci_mmap_mem &&
  721. (rp->flags & IORESOURCE_MEM) == 0))
  722. continue;
  723. }
  724. /* Align the resource end to the next page address.
  725. * PAGE_SIZE intentionally added instead of (PAGE_SIZE - 1),
  726. * because actually we need the address of the next byte
  727. * after rp->end.
  728. */
  729. aligned_end = (rp->end + PAGE_SIZE) & PAGE_MASK;
  730. if ((rp->start <= user_paddr) &&
  731. (user_paddr + user_size) <= aligned_end)
  732. break;
  733. }
  734. if (i > PCI_ROM_RESOURCE)
  735. return -EINVAL;
  736. return 0;
  737. }
  738. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  739. * device mapping.
  740. */
  741. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  742. enum pci_mmap_state mmap_state)
  743. {
  744. /* Our io_remap_pfn_range takes care of this, do nothing. */
  745. }
  746. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  747. * for this architecture. The region in the process to map is described by vm_start
  748. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  749. * The pci device structure is provided so that architectures may make mapping
  750. * decisions on a per-device or per-bus basis.
  751. *
  752. * Returns a negative error code on failure, zero on success.
  753. */
  754. int pci_mmap_page_range(struct pci_dev *dev, int bar,
  755. struct vm_area_struct *vma,
  756. enum pci_mmap_state mmap_state, int write_combine)
  757. {
  758. int ret;
  759. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  760. if (ret < 0)
  761. return ret;
  762. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  763. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  764. ret = io_remap_pfn_range(vma, vma->vm_start,
  765. vma->vm_pgoff,
  766. vma->vm_end - vma->vm_start,
  767. vma->vm_page_prot);
  768. if (ret)
  769. return ret;
  770. return 0;
  771. }
  772. #ifdef CONFIG_NUMA
  773. int pcibus_to_node(struct pci_bus *pbus)
  774. {
  775. struct pci_pbm_info *pbm = pbus->sysdata;
  776. return pbm->numa_node;
  777. }
  778. EXPORT_SYMBOL(pcibus_to_node);
  779. #endif
  780. /* Return the domain number for this pci bus */
  781. int pci_domain_nr(struct pci_bus *pbus)
  782. {
  783. struct pci_pbm_info *pbm = pbus->sysdata;
  784. int ret;
  785. if (!pbm) {
  786. ret = -ENXIO;
  787. } else {
  788. ret = pbm->index;
  789. }
  790. return ret;
  791. }
  792. EXPORT_SYMBOL(pci_domain_nr);
  793. #ifdef CONFIG_PCI_MSI
  794. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  795. {
  796. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  797. unsigned int irq;
  798. if (!pbm->setup_msi_irq)
  799. return -EINVAL;
  800. return pbm->setup_msi_irq(&irq, pdev, desc);
  801. }
  802. void arch_teardown_msi_irq(unsigned int irq)
  803. {
  804. struct msi_desc *entry = irq_get_msi_desc(irq);
  805. struct pci_dev *pdev = msi_desc_to_pci_dev(entry);
  806. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  807. if (pbm->teardown_msi_irq)
  808. pbm->teardown_msi_irq(irq, pdev);
  809. }
  810. #endif /* !(CONFIG_PCI_MSI) */
  811. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  812. {
  813. struct pci_dev *ali_isa_bridge;
  814. u8 val;
  815. /* ALI sound chips generate 31-bits of DMA, a special register
  816. * determines what bit 31 is emitted as.
  817. */
  818. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  819. PCI_DEVICE_ID_AL_M1533,
  820. NULL);
  821. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  822. if (set_bit)
  823. val |= 0x01;
  824. else
  825. val &= ~0x01;
  826. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  827. pci_dev_put(ali_isa_bridge);
  828. }
  829. int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask)
  830. {
  831. u64 dma_addr_mask;
  832. if (pdev == NULL) {
  833. dma_addr_mask = 0xffffffff;
  834. } else {
  835. struct iommu *iommu = pdev->dev.archdata.iommu;
  836. dma_addr_mask = iommu->dma_addr_mask;
  837. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  838. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  839. device_mask == 0x7fffffff) {
  840. ali_sound_dma_hack(pdev,
  841. (dma_addr_mask & 0x80000000) != 0);
  842. return 1;
  843. }
  844. }
  845. if (device_mask >= (1UL << 32UL))
  846. return 0;
  847. return (device_mask & dma_addr_mask) == dma_addr_mask;
  848. }
  849. void pci_resource_to_user(const struct pci_dev *pdev, int bar,
  850. const struct resource *rp, resource_size_t *start,
  851. resource_size_t *end)
  852. {
  853. struct pci_bus_region region;
  854. /*
  855. * "User" addresses are shown in /sys/devices/pci.../.../resource
  856. * and /proc/bus/pci/devices and used as mmap offsets for
  857. * /proc/bus/pci/BB/DD.F files (see proc_bus_pci_mmap()).
  858. *
  859. * On sparc, these are PCI bus addresses, i.e., raw BAR values.
  860. */
  861. pcibios_resource_to_bus(pdev->bus, &region, (struct resource *) rp);
  862. *start = region.start;
  863. *end = region.end;
  864. }
  865. void pcibios_set_master(struct pci_dev *dev)
  866. {
  867. /* No special bus mastering setup handling */
  868. }
  869. #ifdef CONFIG_PCI_IOV
  870. int pcibios_add_device(struct pci_dev *dev)
  871. {
  872. struct pci_dev *pdev;
  873. /* Add sriov arch specific initialization here.
  874. * Copy dev_archdata from PF to VF
  875. */
  876. if (dev->is_virtfn) {
  877. struct dev_archdata *psd;
  878. pdev = dev->physfn;
  879. psd = &pdev->dev.archdata;
  880. pci_init_dev_archdata(&dev->dev.archdata, psd->iommu,
  881. psd->stc, psd->host_controller, NULL,
  882. psd->numa_node);
  883. }
  884. return 0;
  885. }
  886. #endif /* CONFIG_PCI_IOV */
  887. static int __init pcibios_init(void)
  888. {
  889. pci_dfl_cache_line_size = 64 >> 2;
  890. return 0;
  891. }
  892. subsys_initcall(pcibios_init);
  893. #ifdef CONFIG_SYSFS
  894. #define SLOT_NAME_SIZE 11 /* Max decimal digits + null in u32 */
  895. static void pcie_bus_slot_names(struct pci_bus *pbus)
  896. {
  897. struct pci_dev *pdev;
  898. struct pci_bus *bus;
  899. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  900. char name[SLOT_NAME_SIZE];
  901. struct pci_slot *pci_slot;
  902. const u32 *slot_num;
  903. int len;
  904. slot_num = of_get_property(pdev->dev.of_node,
  905. "physical-slot#", &len);
  906. if (slot_num == NULL || len != 4)
  907. continue;
  908. snprintf(name, sizeof(name), "%u", slot_num[0]);
  909. pci_slot = pci_create_slot(pbus, slot_num[0], name, NULL);
  910. if (IS_ERR(pci_slot))
  911. pr_err("PCI: pci_create_slot returned %ld.\n",
  912. PTR_ERR(pci_slot));
  913. }
  914. list_for_each_entry(bus, &pbus->children, node)
  915. pcie_bus_slot_names(bus);
  916. }
  917. static void pci_bus_slot_names(struct device_node *node, struct pci_bus *bus)
  918. {
  919. const struct pci_slot_names {
  920. u32 slot_mask;
  921. char names[0];
  922. } *prop;
  923. const char *sp;
  924. int len, i;
  925. u32 mask;
  926. prop = of_get_property(node, "slot-names", &len);
  927. if (!prop)
  928. return;
  929. mask = prop->slot_mask;
  930. sp = prop->names;
  931. if (ofpci_verbose)
  932. pci_info(bus, "Making slots for [%s] mask[0x%02x]\n",
  933. node->full_name, mask);
  934. i = 0;
  935. while (mask) {
  936. struct pci_slot *pci_slot;
  937. u32 this_bit = 1 << i;
  938. if (!(mask & this_bit)) {
  939. i++;
  940. continue;
  941. }
  942. if (ofpci_verbose)
  943. pci_info(bus, "Making slot [%s]\n", sp);
  944. pci_slot = pci_create_slot(bus, i, sp, NULL);
  945. if (IS_ERR(pci_slot))
  946. pci_err(bus, "pci_create_slot returned %ld\n",
  947. PTR_ERR(pci_slot));
  948. sp += strlen(sp) + 1;
  949. mask &= ~this_bit;
  950. i++;
  951. }
  952. }
  953. static int __init of_pci_slot_init(void)
  954. {
  955. struct pci_bus *pbus = NULL;
  956. while ((pbus = pci_find_next_bus(pbus)) != NULL) {
  957. struct device_node *node;
  958. struct pci_dev *pdev;
  959. pdev = list_first_entry(&pbus->devices, struct pci_dev,
  960. bus_list);
  961. if (pdev && pci_is_pcie(pdev)) {
  962. pcie_bus_slot_names(pbus);
  963. } else {
  964. if (pbus->self) {
  965. /* PCI->PCI bridge */
  966. node = pbus->self->dev.of_node;
  967. } else {
  968. struct pci_pbm_info *pbm = pbus->sysdata;
  969. /* Host PCI controller */
  970. node = pbm->op->dev.of_node;
  971. }
  972. pci_bus_slot_names(node, pbus);
  973. }
  974. }
  975. return 0;
  976. }
  977. device_initcall(of_pci_slot_init);
  978. #endif