pci.c 27 KB

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