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