pci.c 28 KB

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