pci.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 pci_slot *slot;
  223. struct platform_device *op;
  224. struct pci_dev *dev;
  225. const char *type;
  226. u32 class;
  227. dev = pci_alloc_dev(bus);
  228. if (!dev)
  229. return NULL;
  230. sd = &dev->dev.archdata;
  231. sd->iommu = pbm->iommu;
  232. sd->stc = &pbm->stc;
  233. sd->host_controller = pbm;
  234. sd->op = op = of_find_device_by_node(node);
  235. sd->numa_node = pbm->numa_node;
  236. sd = &op->dev.archdata;
  237. sd->iommu = pbm->iommu;
  238. sd->stc = &pbm->stc;
  239. sd->numa_node = pbm->numa_node;
  240. if (!strcmp(node->name, "ebus"))
  241. of_propagate_archdata(op);
  242. type = of_get_property(node, "device_type", NULL);
  243. if (type == NULL)
  244. type = "";
  245. if (ofpci_verbose)
  246. printk(" create device, devfn: %x, type: %s\n",
  247. devfn, type);
  248. dev->sysdata = node;
  249. dev->dev.parent = bus->bridge;
  250. dev->dev.bus = &pci_bus_type;
  251. dev->dev.of_node = of_node_get(node);
  252. dev->devfn = devfn;
  253. dev->multifunction = 0; /* maybe a lie? */
  254. set_pcie_port_type(dev);
  255. list_for_each_entry(slot, &dev->bus->slots, list)
  256. if (PCI_SLOT(dev->devfn) == slot->number)
  257. dev->slot = slot;
  258. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  259. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  260. dev->subsystem_vendor =
  261. of_getintprop_default(node, "subsystem-vendor-id", 0);
  262. dev->subsystem_device =
  263. of_getintprop_default(node, "subsystem-id", 0);
  264. dev->cfg_size = pci_cfg_space_size(dev);
  265. /* We can't actually use the firmware value, we have
  266. * to read what is in the register right now. One
  267. * reason is that in the case of IDE interfaces the
  268. * firmware can sample the value before the the IDE
  269. * interface is programmed into native mode.
  270. */
  271. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  272. dev->class = class >> 8;
  273. dev->revision = class & 0xff;
  274. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  275. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  276. if (ofpci_verbose)
  277. printk(" class: 0x%x device name: %s\n",
  278. dev->class, pci_name(dev));
  279. /* I have seen IDE devices which will not respond to
  280. * the bmdma simplex check reads if bus mastering is
  281. * disabled.
  282. */
  283. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
  284. pci_set_master(dev);
  285. dev->current_state = PCI_UNKNOWN; /* unknown power state */
  286. dev->error_state = pci_channel_io_normal;
  287. dev->dma_mask = 0xffffffff;
  288. if (!strcmp(node->name, "pci")) {
  289. /* a PCI-PCI bridge */
  290. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  291. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  292. } else if (!strcmp(type, "cardbus")) {
  293. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  294. } else {
  295. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  296. dev->rom_base_reg = PCI_ROM_ADDRESS;
  297. dev->irq = sd->op->archdata.irqs[0];
  298. if (dev->irq == 0xffffffff)
  299. dev->irq = PCI_IRQ_NONE;
  300. }
  301. pci_parse_of_addrs(sd->op, node, dev);
  302. if (ofpci_verbose)
  303. printk(" adding to system ...\n");
  304. pci_device_add(dev, bus);
  305. return dev;
  306. }
  307. static void apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
  308. {
  309. u32 idx, first, last;
  310. first = 8;
  311. last = 0;
  312. for (idx = 0; idx < 8; idx++) {
  313. if ((map & (1 << idx)) != 0) {
  314. if (first > idx)
  315. first = idx;
  316. if (last < idx)
  317. last = idx;
  318. }
  319. }
  320. *first_p = first;
  321. *last_p = last;
  322. }
  323. /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  324. * a proper 'ranges' property.
  325. */
  326. static void apb_fake_ranges(struct pci_dev *dev,
  327. struct pci_bus *bus,
  328. struct pci_pbm_info *pbm)
  329. {
  330. struct pci_bus_region region;
  331. struct resource *res;
  332. u32 first, last;
  333. u8 map;
  334. pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
  335. apb_calc_first_last(map, &first, &last);
  336. res = bus->resource[0];
  337. res->flags = IORESOURCE_IO;
  338. region.start = (first << 21);
  339. region.end = (last << 21) + ((1 << 21) - 1);
  340. pcibios_bus_to_resource(dev->bus, res, &region);
  341. pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
  342. apb_calc_first_last(map, &first, &last);
  343. res = bus->resource[1];
  344. res->flags = IORESOURCE_MEM;
  345. region.start = (first << 29);
  346. region.end = (last << 29) + ((1 << 29) - 1);
  347. pcibios_bus_to_resource(dev->bus, res, &region);
  348. }
  349. static void pci_of_scan_bus(struct pci_pbm_info *pbm,
  350. struct device_node *node,
  351. struct pci_bus *bus);
  352. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  353. static void of_scan_pci_bridge(struct pci_pbm_info *pbm,
  354. struct device_node *node,
  355. struct pci_dev *dev)
  356. {
  357. struct pci_bus *bus;
  358. const u32 *busrange, *ranges;
  359. int len, i, simba;
  360. struct pci_bus_region region;
  361. struct resource *res;
  362. unsigned int flags;
  363. u64 size;
  364. if (ofpci_verbose)
  365. printk("of_scan_pci_bridge(%s)\n", node->full_name);
  366. /* parse bus-range property */
  367. busrange = of_get_property(node, "bus-range", &len);
  368. if (busrange == NULL || len != 8) {
  369. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  370. node->full_name);
  371. return;
  372. }
  373. if (ofpci_verbose)
  374. printk(" Bridge bus range [%u --> %u]\n",
  375. busrange[0], busrange[1]);
  376. ranges = of_get_property(node, "ranges", &len);
  377. simba = 0;
  378. if (ranges == NULL) {
  379. const char *model = of_get_property(node, "model", NULL);
  380. if (model && !strcmp(model, "SUNW,simba"))
  381. simba = 1;
  382. }
  383. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  384. if (!bus) {
  385. printk(KERN_ERR "Failed to create pci bus for %s\n",
  386. node->full_name);
  387. return;
  388. }
  389. bus->primary = dev->bus->number;
  390. pci_bus_insert_busn_res(bus, busrange[0], busrange[1]);
  391. bus->bridge_ctl = 0;
  392. if (ofpci_verbose)
  393. printk(" Bridge ranges[%p] simba[%d]\n",
  394. ranges, simba);
  395. /* parse ranges property, or cook one up by hand for Simba */
  396. /* PCI #address-cells == 3 and #size-cells == 2 always */
  397. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  398. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  399. res->flags = 0;
  400. bus->resource[i] = res;
  401. ++res;
  402. }
  403. if (simba) {
  404. apb_fake_ranges(dev, bus, pbm);
  405. goto after_ranges;
  406. } else if (ranges == NULL) {
  407. pci_read_bridge_bases(bus);
  408. goto after_ranges;
  409. }
  410. i = 1;
  411. for (; len >= 32; len -= 32, ranges += 8) {
  412. u64 start;
  413. if (ofpci_verbose)
  414. printk(" RAW Range[%08x:%08x:%08x:%08x:%08x:%08x:"
  415. "%08x:%08x]\n",
  416. ranges[0], ranges[1], ranges[2], ranges[3],
  417. ranges[4], ranges[5], ranges[6], ranges[7]);
  418. flags = pci_parse_of_flags(ranges[0]);
  419. size = GET_64BIT(ranges, 6);
  420. if (flags == 0 || size == 0)
  421. continue;
  422. /* On PCI-Express systems, PCI bridges that have no devices downstream
  423. * have a bogus size value where the first 32-bit cell is 0xffffffff.
  424. * This results in a bogus range where start + size overflows.
  425. *
  426. * Just skip these otherwise the kernel will complain when the resource
  427. * tries to be claimed.
  428. */
  429. if (size >> 32 == 0xffffffff)
  430. continue;
  431. if (flags & IORESOURCE_IO) {
  432. res = bus->resource[0];
  433. if (res->flags) {
  434. printk(KERN_ERR "PCI: ignoring extra I/O range"
  435. " for bridge %s\n", node->full_name);
  436. continue;
  437. }
  438. } else {
  439. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  440. printk(KERN_ERR "PCI: too many memory ranges"
  441. " for bridge %s\n", node->full_name);
  442. continue;
  443. }
  444. res = bus->resource[i];
  445. ++i;
  446. }
  447. res->flags = flags;
  448. region.start = start = GET_64BIT(ranges, 1);
  449. region.end = region.start + size - 1;
  450. if (ofpci_verbose)
  451. printk(" Using flags[%08x] start[%016llx] size[%016llx]\n",
  452. flags, start, size);
  453. pcibios_bus_to_resource(dev->bus, res, &region);
  454. }
  455. after_ranges:
  456. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  457. bus->number);
  458. if (ofpci_verbose)
  459. printk(" bus name: %s\n", bus->name);
  460. pci_of_scan_bus(pbm, node, bus);
  461. }
  462. static void pci_of_scan_bus(struct pci_pbm_info *pbm,
  463. struct device_node *node,
  464. struct pci_bus *bus)
  465. {
  466. struct device_node *child;
  467. const u32 *reg;
  468. int reglen, devfn, prev_devfn;
  469. struct pci_dev *dev;
  470. if (ofpci_verbose)
  471. printk("PCI: scan_bus[%s] bus no %d\n",
  472. node->full_name, bus->number);
  473. child = NULL;
  474. prev_devfn = -1;
  475. while ((child = of_get_next_child(node, child)) != NULL) {
  476. if (ofpci_verbose)
  477. printk(" * %s\n", child->full_name);
  478. reg = of_get_property(child, "reg", &reglen);
  479. if (reg == NULL || reglen < 20)
  480. continue;
  481. devfn = (reg[0] >> 8) & 0xff;
  482. /* This is a workaround for some device trees
  483. * which list PCI devices twice. On the V100
  484. * for example, device number 3 is listed twice.
  485. * Once as "pm" and once again as "lomp".
  486. */
  487. if (devfn == prev_devfn)
  488. continue;
  489. prev_devfn = devfn;
  490. /* create a new pci_dev for this device */
  491. dev = of_create_pci_dev(pbm, child, bus, devfn);
  492. if (!dev)
  493. continue;
  494. if (ofpci_verbose)
  495. printk("PCI: dev header type: %x\n",
  496. dev->hdr_type);
  497. if (pci_is_bridge(dev))
  498. of_scan_pci_bridge(pbm, child, dev);
  499. }
  500. }
  501. static ssize_t
  502. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  503. {
  504. struct pci_dev *pdev;
  505. struct device_node *dp;
  506. pdev = to_pci_dev(dev);
  507. dp = pdev->dev.of_node;
  508. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  509. }
  510. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  511. static void pci_bus_register_of_sysfs(struct pci_bus *bus)
  512. {
  513. struct pci_dev *dev;
  514. struct pci_bus *child_bus;
  515. int err;
  516. list_for_each_entry(dev, &bus->devices, bus_list) {
  517. /* we don't really care if we can create this file or
  518. * not, but we need to assign the result of the call
  519. * or the world will fall under alien invasion and
  520. * everybody will be frozen on a spaceship ready to be
  521. * eaten on alpha centauri by some green and jelly
  522. * humanoid.
  523. */
  524. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  525. (void) err;
  526. }
  527. list_for_each_entry(child_bus, &bus->children, node)
  528. pci_bus_register_of_sysfs(child_bus);
  529. }
  530. static void pci_claim_bus_resources(struct pci_bus *bus)
  531. {
  532. struct pci_bus *child_bus;
  533. struct pci_dev *dev;
  534. list_for_each_entry(dev, &bus->devices, bus_list) {
  535. int i;
  536. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  537. struct resource *r = &dev->resource[i];
  538. if (r->parent || !r->start || !r->flags)
  539. continue;
  540. if (ofpci_verbose)
  541. printk("PCI: Claiming %s: "
  542. "Resource %d: %016llx..%016llx [%x]\n",
  543. pci_name(dev), i,
  544. (unsigned long long)r->start,
  545. (unsigned long long)r->end,
  546. (unsigned int)r->flags);
  547. pci_claim_resource(dev, i);
  548. }
  549. }
  550. list_for_each_entry(child_bus, &bus->children, node)
  551. pci_claim_bus_resources(child_bus);
  552. }
  553. struct pci_bus *pci_scan_one_pbm(struct pci_pbm_info *pbm,
  554. struct device *parent)
  555. {
  556. LIST_HEAD(resources);
  557. struct device_node *node = pbm->op->dev.of_node;
  558. struct pci_bus *bus;
  559. printk("PCI: Scanning PBM %s\n", node->full_name);
  560. pci_add_resource_offset(&resources, &pbm->io_space,
  561. pbm->io_space.start);
  562. pci_add_resource_offset(&resources, &pbm->mem_space,
  563. pbm->mem_space.start);
  564. pbm->busn.start = pbm->pci_first_busno;
  565. pbm->busn.end = pbm->pci_last_busno;
  566. pbm->busn.flags = IORESOURCE_BUS;
  567. pci_add_resource(&resources, &pbm->busn);
  568. bus = pci_create_root_bus(parent, pbm->pci_first_busno, pbm->pci_ops,
  569. pbm, &resources);
  570. if (!bus) {
  571. printk(KERN_ERR "Failed to create bus for %s\n",
  572. node->full_name);
  573. pci_free_resource_list(&resources);
  574. return NULL;
  575. }
  576. pci_of_scan_bus(pbm, node, bus);
  577. pci_bus_register_of_sysfs(bus);
  578. pci_claim_bus_resources(bus);
  579. pci_bus_add_devices(bus);
  580. return bus;
  581. }
  582. void pcibios_fixup_bus(struct pci_bus *pbus)
  583. {
  584. }
  585. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  586. resource_size_t size, resource_size_t align)
  587. {
  588. return res->start;
  589. }
  590. int pcibios_enable_device(struct pci_dev *dev, int mask)
  591. {
  592. u16 cmd, oldcmd;
  593. int i;
  594. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  595. oldcmd = cmd;
  596. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  597. struct resource *res = &dev->resource[i];
  598. /* Only set up the requested stuff */
  599. if (!(mask & (1<<i)))
  600. continue;
  601. if (res->flags & IORESOURCE_IO)
  602. cmd |= PCI_COMMAND_IO;
  603. if (res->flags & IORESOURCE_MEM)
  604. cmd |= PCI_COMMAND_MEMORY;
  605. }
  606. if (cmd != oldcmd) {
  607. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  608. pci_name(dev), cmd);
  609. /* Enable the appropriate bits in the PCI command register. */
  610. pci_write_config_word(dev, PCI_COMMAND, cmd);
  611. }
  612. return 0;
  613. }
  614. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  615. /* If the user uses a host-bridge as the PCI device, he may use
  616. * this to perform a raw mmap() of the I/O or MEM space behind
  617. * that controller.
  618. *
  619. * This can be useful for execution of x86 PCI bios initialization code
  620. * on a PCI card, like the xfree86 int10 stuff does.
  621. */
  622. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  623. enum pci_mmap_state mmap_state)
  624. {
  625. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  626. unsigned long space_size, user_offset, user_size;
  627. if (mmap_state == pci_mmap_io) {
  628. space_size = resource_size(&pbm->io_space);
  629. } else {
  630. space_size = resource_size(&pbm->mem_space);
  631. }
  632. /* Make sure the request is in range. */
  633. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  634. user_size = vma->vm_end - vma->vm_start;
  635. if (user_offset >= space_size ||
  636. (user_offset + user_size) > space_size)
  637. return -EINVAL;
  638. if (mmap_state == pci_mmap_io) {
  639. vma->vm_pgoff = (pbm->io_space.start +
  640. user_offset) >> PAGE_SHIFT;
  641. } else {
  642. vma->vm_pgoff = (pbm->mem_space.start +
  643. user_offset) >> PAGE_SHIFT;
  644. }
  645. return 0;
  646. }
  647. /* Adjust vm_pgoff of VMA such that it is the physical page offset
  648. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  649. *
  650. * Basically, the user finds the base address for his device which he wishes
  651. * to mmap. They read the 32-bit value from the config space base register,
  652. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  653. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  654. *
  655. * Returns negative error code on failure, zero on success.
  656. */
  657. static int __pci_mmap_make_offset(struct pci_dev *pdev,
  658. struct vm_area_struct *vma,
  659. enum pci_mmap_state mmap_state)
  660. {
  661. unsigned long user_paddr, user_size;
  662. int i, err;
  663. /* First compute the physical address in vma->vm_pgoff,
  664. * making sure the user offset is within range in the
  665. * appropriate PCI space.
  666. */
  667. err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
  668. if (err)
  669. return err;
  670. /* If this is a mapping on a host bridge, any address
  671. * is OK.
  672. */
  673. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  674. return err;
  675. /* Otherwise make sure it's in the range for one of the
  676. * device's resources.
  677. */
  678. user_paddr = vma->vm_pgoff << PAGE_SHIFT;
  679. user_size = vma->vm_end - vma->vm_start;
  680. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  681. struct resource *rp = &pdev->resource[i];
  682. resource_size_t aligned_end;
  683. /* Active? */
  684. if (!rp->flags)
  685. continue;
  686. /* Same type? */
  687. if (i == PCI_ROM_RESOURCE) {
  688. if (mmap_state != pci_mmap_mem)
  689. continue;
  690. } else {
  691. if ((mmap_state == pci_mmap_io &&
  692. (rp->flags & IORESOURCE_IO) == 0) ||
  693. (mmap_state == pci_mmap_mem &&
  694. (rp->flags & IORESOURCE_MEM) == 0))
  695. continue;
  696. }
  697. /* Align the resource end to the next page address.
  698. * PAGE_SIZE intentionally added instead of (PAGE_SIZE - 1),
  699. * because actually we need the address of the next byte
  700. * after rp->end.
  701. */
  702. aligned_end = (rp->end + PAGE_SIZE) & PAGE_MASK;
  703. if ((rp->start <= user_paddr) &&
  704. (user_paddr + user_size) <= aligned_end)
  705. break;
  706. }
  707. if (i > PCI_ROM_RESOURCE)
  708. return -EINVAL;
  709. return 0;
  710. }
  711. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  712. * device mapping.
  713. */
  714. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  715. enum pci_mmap_state mmap_state)
  716. {
  717. /* Our io_remap_pfn_range takes care of this, do nothing. */
  718. }
  719. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  720. * for this architecture. The region in the process to map is described by vm_start
  721. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  722. * The pci device structure is provided so that architectures may make mapping
  723. * decisions on a per-device or per-bus basis.
  724. *
  725. * Returns a negative error code on failure, zero on success.
  726. */
  727. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  728. enum pci_mmap_state mmap_state,
  729. int write_combine)
  730. {
  731. int ret;
  732. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  733. if (ret < 0)
  734. return ret;
  735. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  736. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  737. ret = io_remap_pfn_range(vma, vma->vm_start,
  738. vma->vm_pgoff,
  739. vma->vm_end - vma->vm_start,
  740. vma->vm_page_prot);
  741. if (ret)
  742. return ret;
  743. return 0;
  744. }
  745. #ifdef CONFIG_NUMA
  746. int pcibus_to_node(struct pci_bus *pbus)
  747. {
  748. struct pci_pbm_info *pbm = pbus->sysdata;
  749. return pbm->numa_node;
  750. }
  751. EXPORT_SYMBOL(pcibus_to_node);
  752. #endif
  753. /* Return the domain number for this pci bus */
  754. int pci_domain_nr(struct pci_bus *pbus)
  755. {
  756. struct pci_pbm_info *pbm = pbus->sysdata;
  757. int ret;
  758. if (!pbm) {
  759. ret = -ENXIO;
  760. } else {
  761. ret = pbm->index;
  762. }
  763. return ret;
  764. }
  765. EXPORT_SYMBOL(pci_domain_nr);
  766. #ifdef CONFIG_PCI_MSI
  767. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  768. {
  769. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  770. unsigned int irq;
  771. if (!pbm->setup_msi_irq)
  772. return -EINVAL;
  773. return pbm->setup_msi_irq(&irq, pdev, desc);
  774. }
  775. void arch_teardown_msi_irq(unsigned int irq)
  776. {
  777. struct msi_desc *entry = irq_get_msi_desc(irq);
  778. struct pci_dev *pdev = entry->dev;
  779. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  780. if (pbm->teardown_msi_irq)
  781. pbm->teardown_msi_irq(irq, pdev);
  782. }
  783. #endif /* !(CONFIG_PCI_MSI) */
  784. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  785. {
  786. struct pci_dev *ali_isa_bridge;
  787. u8 val;
  788. /* ALI sound chips generate 31-bits of DMA, a special register
  789. * determines what bit 31 is emitted as.
  790. */
  791. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  792. PCI_DEVICE_ID_AL_M1533,
  793. NULL);
  794. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  795. if (set_bit)
  796. val |= 0x01;
  797. else
  798. val &= ~0x01;
  799. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  800. pci_dev_put(ali_isa_bridge);
  801. }
  802. int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask)
  803. {
  804. u64 dma_addr_mask;
  805. if (pdev == NULL) {
  806. dma_addr_mask = 0xffffffff;
  807. } else {
  808. struct iommu *iommu = pdev->dev.archdata.iommu;
  809. dma_addr_mask = iommu->dma_addr_mask;
  810. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  811. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  812. device_mask == 0x7fffffff) {
  813. ali_sound_dma_hack(pdev,
  814. (dma_addr_mask & 0x80000000) != 0);
  815. return 1;
  816. }
  817. }
  818. if (device_mask >= (1UL << 32UL))
  819. return 0;
  820. return (device_mask & dma_addr_mask) == dma_addr_mask;
  821. }
  822. void pci_resource_to_user(const struct pci_dev *pdev, int bar,
  823. const struct resource *rp, resource_size_t *start,
  824. resource_size_t *end)
  825. {
  826. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  827. unsigned long offset;
  828. if (rp->flags & IORESOURCE_IO)
  829. offset = pbm->io_space.start;
  830. else
  831. offset = pbm->mem_space.start;
  832. *start = rp->start - offset;
  833. *end = rp->end - offset;
  834. }
  835. void pcibios_set_master(struct pci_dev *dev)
  836. {
  837. /* No special bus mastering setup handling */
  838. }
  839. static int __init pcibios_init(void)
  840. {
  841. pci_dfl_cache_line_size = 64 >> 2;
  842. return 0;
  843. }
  844. subsys_initcall(pcibios_init);
  845. #ifdef CONFIG_SYSFS
  846. #define SLOT_NAME_SIZE 11 /* Max decimal digits + null in u32 */
  847. static void pcie_bus_slot_names(struct pci_bus *pbus)
  848. {
  849. struct pci_dev *pdev;
  850. struct pci_bus *bus;
  851. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  852. char name[SLOT_NAME_SIZE];
  853. struct pci_slot *pci_slot;
  854. const u32 *slot_num;
  855. int len;
  856. slot_num = of_get_property(pdev->dev.of_node,
  857. "physical-slot#", &len);
  858. if (slot_num == NULL || len != 4)
  859. continue;
  860. snprintf(name, sizeof(name), "%u", slot_num[0]);
  861. pci_slot = pci_create_slot(pbus, slot_num[0], name, NULL);
  862. if (IS_ERR(pci_slot))
  863. pr_err("PCI: pci_create_slot returned %ld.\n",
  864. PTR_ERR(pci_slot));
  865. }
  866. list_for_each_entry(bus, &pbus->children, node)
  867. pcie_bus_slot_names(bus);
  868. }
  869. static void pci_bus_slot_names(struct device_node *node, struct pci_bus *bus)
  870. {
  871. const struct pci_slot_names {
  872. u32 slot_mask;
  873. char names[0];
  874. } *prop;
  875. const char *sp;
  876. int len, i;
  877. u32 mask;
  878. prop = of_get_property(node, "slot-names", &len);
  879. if (!prop)
  880. return;
  881. mask = prop->slot_mask;
  882. sp = prop->names;
  883. if (ofpci_verbose)
  884. printk("PCI: Making slots for [%s] mask[0x%02x]\n",
  885. node->full_name, mask);
  886. i = 0;
  887. while (mask) {
  888. struct pci_slot *pci_slot;
  889. u32 this_bit = 1 << i;
  890. if (!(mask & this_bit)) {
  891. i++;
  892. continue;
  893. }
  894. if (ofpci_verbose)
  895. printk("PCI: Making slot [%s]\n", sp);
  896. pci_slot = pci_create_slot(bus, i, sp, NULL);
  897. if (IS_ERR(pci_slot))
  898. printk(KERN_ERR "PCI: pci_create_slot returned %ld\n",
  899. PTR_ERR(pci_slot));
  900. sp += strlen(sp) + 1;
  901. mask &= ~this_bit;
  902. i++;
  903. }
  904. }
  905. static int __init of_pci_slot_init(void)
  906. {
  907. struct pci_bus *pbus = NULL;
  908. while ((pbus = pci_find_next_bus(pbus)) != NULL) {
  909. struct device_node *node;
  910. struct pci_dev *pdev;
  911. pdev = list_first_entry(&pbus->devices, struct pci_dev,
  912. bus_list);
  913. if (pdev && pci_is_pcie(pdev)) {
  914. pcie_bus_slot_names(pbus);
  915. } else {
  916. if (pbus->self) {
  917. /* PCI->PCI bridge */
  918. node = pbus->self->dev.of_node;
  919. } else {
  920. struct pci_pbm_info *pbm = pbus->sysdata;
  921. /* Host PCI controller */
  922. node = pbm->op->dev.of_node;
  923. }
  924. pci_bus_slot_names(node, pbus);
  925. }
  926. }
  927. return 0;
  928. }
  929. device_initcall(of_pci_slot_init);
  930. #endif