pci.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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/module.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. /* Probe for all PCI controllers in the system. */
  148. extern void sabre_init(struct device_node *, const char *);
  149. extern void psycho_init(struct device_node *, const char *);
  150. extern void fire_pci_init(struct device_node *, const char *);
  151. static struct {
  152. char *model_name;
  153. void (*init)(struct device_node *, const char *);
  154. } pci_controller_table[] __initdata = {
  155. { "SUNW,sabre", sabre_init },
  156. { "pci108e,a000", sabre_init },
  157. { "pci108e,a001", sabre_init },
  158. { "SUNW,psycho", psycho_init },
  159. { "pci108e,8000", psycho_init },
  160. { "pciex108e,80f0", fire_pci_init },
  161. };
  162. #define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
  163. static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
  164. {
  165. int i;
  166. for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
  167. if (!strncmp(model_name,
  168. pci_controller_table[i].model_name,
  169. namelen)) {
  170. pci_controller_table[i].init(dp, model_name);
  171. return 1;
  172. }
  173. }
  174. return 0;
  175. }
  176. static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
  177. {
  178. struct device_node *dp;
  179. int count = 0;
  180. for_each_node_by_name(dp, "pci") {
  181. struct property *prop;
  182. int len;
  183. prop = of_find_property(dp, "model", &len);
  184. if (!prop)
  185. prop = of_find_property(dp, "compatible", &len);
  186. if (prop) {
  187. const char *model = prop->value;
  188. int item_len = 0;
  189. /* Our value may be a multi-valued string in the
  190. * case of some compatible properties. For sanity,
  191. * only try the first one.
  192. */
  193. while (model[item_len] && len) {
  194. len--;
  195. item_len++;
  196. }
  197. if (handler(model, item_len, dp))
  198. count++;
  199. }
  200. }
  201. return count;
  202. }
  203. /* Find each controller in the system, attach and initialize
  204. * software state structure for each and link into the
  205. * pci_pbm_root. Setup the controller enough such
  206. * that bus scanning can be done.
  207. */
  208. static void __init pci_controller_probe(void)
  209. {
  210. printk("PCI: Probing for controllers.\n");
  211. pci_controller_scan(pci_controller_init);
  212. }
  213. static int ofpci_verbose;
  214. static int __init ofpci_debug(char *str)
  215. {
  216. int val = 0;
  217. get_option(&str, &val);
  218. if (val)
  219. ofpci_verbose = 1;
  220. return 1;
  221. }
  222. __setup("ofpci_debug=", ofpci_debug);
  223. static unsigned long pci_parse_of_flags(u32 addr0)
  224. {
  225. unsigned long flags = 0;
  226. if (addr0 & 0x02000000) {
  227. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  228. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  229. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  230. if (addr0 & 0x40000000)
  231. flags |= IORESOURCE_PREFETCH
  232. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  233. } else if (addr0 & 0x01000000)
  234. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  235. return flags;
  236. }
  237. /* The of_device layer has translated all of the assigned-address properties
  238. * into physical address resources, we only have to figure out the register
  239. * mapping.
  240. */
  241. static void pci_parse_of_addrs(struct of_device *op,
  242. struct device_node *node,
  243. struct pci_dev *dev)
  244. {
  245. struct resource *op_res;
  246. const u32 *addrs;
  247. int proplen;
  248. addrs = of_get_property(node, "assigned-addresses", &proplen);
  249. if (!addrs)
  250. return;
  251. if (ofpci_verbose)
  252. printk(" parse addresses (%d bytes) @ %p\n",
  253. proplen, addrs);
  254. op_res = &op->resource[0];
  255. for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
  256. struct resource *res;
  257. unsigned long flags;
  258. int i;
  259. flags = pci_parse_of_flags(addrs[0]);
  260. if (!flags)
  261. continue;
  262. i = addrs[0] & 0xff;
  263. if (ofpci_verbose)
  264. printk(" start: %lx, end: %lx, i: %x\n",
  265. op_res->start, op_res->end, i);
  266. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  267. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  268. } else if (i == dev->rom_base_reg) {
  269. res = &dev->resource[PCI_ROM_RESOURCE];
  270. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  271. } else {
  272. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  273. continue;
  274. }
  275. res->start = op_res->start;
  276. res->end = op_res->end;
  277. res->flags = flags;
  278. res->name = pci_name(dev);
  279. }
  280. }
  281. struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  282. struct device_node *node,
  283. struct pci_bus *bus, int devfn)
  284. {
  285. struct dev_archdata *sd;
  286. struct of_device *op;
  287. struct pci_dev *dev;
  288. const char *type;
  289. u32 class;
  290. dev = alloc_pci_dev();
  291. if (!dev)
  292. return NULL;
  293. sd = &dev->dev.archdata;
  294. sd->iommu = pbm->iommu;
  295. sd->stc = &pbm->stc;
  296. sd->host_controller = pbm;
  297. sd->prom_node = node;
  298. sd->op = op = of_find_device_by_node(node);
  299. sd->numa_node = pbm->numa_node;
  300. sd = &op->dev.archdata;
  301. sd->iommu = pbm->iommu;
  302. sd->stc = &pbm->stc;
  303. sd->numa_node = pbm->numa_node;
  304. if (!strcmp(node->name, "ebus"))
  305. of_propagate_archdata(op);
  306. type = of_get_property(node, "device_type", NULL);
  307. if (type == NULL)
  308. type = "";
  309. if (ofpci_verbose)
  310. printk(" create device, devfn: %x, type: %s\n",
  311. devfn, type);
  312. dev->bus = bus;
  313. dev->sysdata = node;
  314. dev->dev.parent = bus->bridge;
  315. dev->dev.bus = &pci_bus_type;
  316. dev->devfn = devfn;
  317. dev->multifunction = 0; /* maybe a lie? */
  318. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  319. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  320. dev->subsystem_vendor =
  321. of_getintprop_default(node, "subsystem-vendor-id", 0);
  322. dev->subsystem_device =
  323. of_getintprop_default(node, "subsystem-id", 0);
  324. dev->cfg_size = pci_cfg_space_size(dev);
  325. /* We can't actually use the firmware value, we have
  326. * to read what is in the register right now. One
  327. * reason is that in the case of IDE interfaces the
  328. * firmware can sample the value before the the IDE
  329. * interface is programmed into native mode.
  330. */
  331. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  332. dev->class = class >> 8;
  333. dev->revision = class & 0xff;
  334. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  335. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  336. if (ofpci_verbose)
  337. printk(" class: 0x%x device name: %s\n",
  338. dev->class, pci_name(dev));
  339. /* I have seen IDE devices which will not respond to
  340. * the bmdma simplex check reads if bus mastering is
  341. * disabled.
  342. */
  343. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
  344. pci_set_master(dev);
  345. dev->current_state = 4; /* unknown power state */
  346. dev->error_state = pci_channel_io_normal;
  347. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  348. /* a PCI-PCI bridge */
  349. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  350. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  351. } else if (!strcmp(type, "cardbus")) {
  352. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  353. } else {
  354. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  355. dev->rom_base_reg = PCI_ROM_ADDRESS;
  356. dev->irq = sd->op->irqs[0];
  357. if (dev->irq == 0xffffffff)
  358. dev->irq = PCI_IRQ_NONE;
  359. }
  360. pci_parse_of_addrs(sd->op, node, dev);
  361. if (ofpci_verbose)
  362. printk(" adding to system ...\n");
  363. pci_device_add(dev, bus);
  364. return dev;
  365. }
  366. static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
  367. {
  368. u32 idx, first, last;
  369. first = 8;
  370. last = 0;
  371. for (idx = 0; idx < 8; idx++) {
  372. if ((map & (1 << idx)) != 0) {
  373. if (first > idx)
  374. first = idx;
  375. if (last < idx)
  376. last = idx;
  377. }
  378. }
  379. *first_p = first;
  380. *last_p = last;
  381. }
  382. static void pci_resource_adjust(struct resource *res,
  383. struct resource *root)
  384. {
  385. res->start += root->start;
  386. res->end += root->start;
  387. }
  388. /* For PCI bus devices which lack a 'ranges' property we interrogate
  389. * the config space values to set the resources, just like the generic
  390. * Linux PCI probing code does.
  391. */
  392. static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
  393. struct pci_bus *bus,
  394. struct pci_pbm_info *pbm)
  395. {
  396. struct resource *res;
  397. u8 io_base_lo, io_limit_lo;
  398. u16 mem_base_lo, mem_limit_lo;
  399. unsigned long base, limit;
  400. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  401. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  402. base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
  403. limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
  404. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  405. u16 io_base_hi, io_limit_hi;
  406. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  407. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  408. base |= (io_base_hi << 16);
  409. limit |= (io_limit_hi << 16);
  410. }
  411. res = bus->resource[0];
  412. if (base <= limit) {
  413. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  414. if (!res->start)
  415. res->start = base;
  416. if (!res->end)
  417. res->end = limit + 0xfff;
  418. pci_resource_adjust(res, &pbm->io_space);
  419. }
  420. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  421. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  422. base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  423. limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  424. res = bus->resource[1];
  425. if (base <= limit) {
  426. res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
  427. IORESOURCE_MEM);
  428. res->start = base;
  429. res->end = limit + 0xfffff;
  430. pci_resource_adjust(res, &pbm->mem_space);
  431. }
  432. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  433. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  434. base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  435. limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  436. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  437. u32 mem_base_hi, mem_limit_hi;
  438. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  439. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  440. /*
  441. * Some bridges set the base > limit by default, and some
  442. * (broken) BIOSes do not initialize them. If we find
  443. * this, just assume they are not being used.
  444. */
  445. if (mem_base_hi <= mem_limit_hi) {
  446. base |= ((long) mem_base_hi) << 32;
  447. limit |= ((long) mem_limit_hi) << 32;
  448. }
  449. }
  450. res = bus->resource[2];
  451. if (base <= limit) {
  452. res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
  453. IORESOURCE_MEM | IORESOURCE_PREFETCH);
  454. res->start = base;
  455. res->end = limit + 0xfffff;
  456. pci_resource_adjust(res, &pbm->mem_space);
  457. }
  458. }
  459. /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  460. * a proper 'ranges' property.
  461. */
  462. static void __devinit apb_fake_ranges(struct pci_dev *dev,
  463. struct pci_bus *bus,
  464. struct pci_pbm_info *pbm)
  465. {
  466. struct resource *res;
  467. u32 first, last;
  468. u8 map;
  469. pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
  470. apb_calc_first_last(map, &first, &last);
  471. res = bus->resource[0];
  472. res->start = (first << 21);
  473. res->end = (last << 21) + ((1 << 21) - 1);
  474. res->flags = IORESOURCE_IO;
  475. pci_resource_adjust(res, &pbm->io_space);
  476. pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
  477. apb_calc_first_last(map, &first, &last);
  478. res = bus->resource[1];
  479. res->start = (first << 21);
  480. res->end = (last << 21) + ((1 << 21) - 1);
  481. res->flags = IORESOURCE_MEM;
  482. pci_resource_adjust(res, &pbm->mem_space);
  483. }
  484. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  485. struct device_node *node,
  486. struct pci_bus *bus);
  487. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  488. static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
  489. struct device_node *node,
  490. struct pci_dev *dev)
  491. {
  492. struct pci_bus *bus;
  493. const u32 *busrange, *ranges;
  494. int len, i, simba;
  495. struct resource *res;
  496. unsigned int flags;
  497. u64 size;
  498. if (ofpci_verbose)
  499. printk("of_scan_pci_bridge(%s)\n", node->full_name);
  500. /* parse bus-range property */
  501. busrange = of_get_property(node, "bus-range", &len);
  502. if (busrange == NULL || len != 8) {
  503. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  504. node->full_name);
  505. return;
  506. }
  507. ranges = of_get_property(node, "ranges", &len);
  508. simba = 0;
  509. if (ranges == NULL) {
  510. const char *model = of_get_property(node, "model", NULL);
  511. if (model && !strcmp(model, "SUNW,simba"))
  512. simba = 1;
  513. }
  514. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  515. if (!bus) {
  516. printk(KERN_ERR "Failed to create pci bus for %s\n",
  517. node->full_name);
  518. return;
  519. }
  520. bus->primary = dev->bus->number;
  521. bus->subordinate = busrange[1];
  522. bus->bridge_ctl = 0;
  523. /* parse ranges property, or cook one up by hand for Simba */
  524. /* PCI #address-cells == 3 and #size-cells == 2 always */
  525. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  526. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  527. res->flags = 0;
  528. bus->resource[i] = res;
  529. ++res;
  530. }
  531. if (simba) {
  532. apb_fake_ranges(dev, bus, pbm);
  533. goto after_ranges;
  534. } else if (ranges == NULL) {
  535. pci_cfg_fake_ranges(dev, bus, pbm);
  536. goto after_ranges;
  537. }
  538. i = 1;
  539. for (; len >= 32; len -= 32, ranges += 8) {
  540. struct resource *root;
  541. flags = pci_parse_of_flags(ranges[0]);
  542. size = GET_64BIT(ranges, 6);
  543. if (flags == 0 || size == 0)
  544. continue;
  545. if (flags & IORESOURCE_IO) {
  546. res = bus->resource[0];
  547. if (res->flags) {
  548. printk(KERN_ERR "PCI: ignoring extra I/O range"
  549. " for bridge %s\n", node->full_name);
  550. continue;
  551. }
  552. root = &pbm->io_space;
  553. } else {
  554. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  555. printk(KERN_ERR "PCI: too many memory ranges"
  556. " for bridge %s\n", node->full_name);
  557. continue;
  558. }
  559. res = bus->resource[i];
  560. ++i;
  561. root = &pbm->mem_space;
  562. }
  563. res->start = GET_64BIT(ranges, 1);
  564. res->end = res->start + size - 1;
  565. res->flags = flags;
  566. /* Another way to implement this would be to add an of_device
  567. * layer routine that can calculate a resource for a given
  568. * range property value in a PCI device.
  569. */
  570. pci_resource_adjust(res, root);
  571. }
  572. after_ranges:
  573. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  574. bus->number);
  575. if (ofpci_verbose)
  576. printk(" bus name: %s\n", bus->name);
  577. pci_of_scan_bus(pbm, node, bus);
  578. }
  579. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  580. struct device_node *node,
  581. struct pci_bus *bus)
  582. {
  583. struct device_node *child;
  584. const u32 *reg;
  585. int reglen, devfn, prev_devfn;
  586. struct pci_dev *dev;
  587. if (ofpci_verbose)
  588. printk("PCI: scan_bus[%s] bus no %d\n",
  589. node->full_name, bus->number);
  590. child = NULL;
  591. prev_devfn = -1;
  592. while ((child = of_get_next_child(node, child)) != NULL) {
  593. if (ofpci_verbose)
  594. printk(" * %s\n", child->full_name);
  595. reg = of_get_property(child, "reg", &reglen);
  596. if (reg == NULL || reglen < 20)
  597. continue;
  598. devfn = (reg[0] >> 8) & 0xff;
  599. /* This is a workaround for some device trees
  600. * which list PCI devices twice. On the V100
  601. * for example, device number 3 is listed twice.
  602. * Once as "pm" and once again as "lomp".
  603. */
  604. if (devfn == prev_devfn)
  605. continue;
  606. prev_devfn = devfn;
  607. /* create a new pci_dev for this device */
  608. dev = of_create_pci_dev(pbm, child, bus, devfn);
  609. if (!dev)
  610. continue;
  611. if (ofpci_verbose)
  612. printk("PCI: dev header type: %x\n",
  613. dev->hdr_type);
  614. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  615. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  616. of_scan_pci_bridge(pbm, child, dev);
  617. }
  618. }
  619. static ssize_t
  620. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  621. {
  622. struct pci_dev *pdev;
  623. struct device_node *dp;
  624. pdev = to_pci_dev(dev);
  625. dp = pdev->dev.archdata.prom_node;
  626. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  627. }
  628. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  629. static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
  630. {
  631. struct pci_dev *dev;
  632. struct pci_bus *child_bus;
  633. int err;
  634. list_for_each_entry(dev, &bus->devices, bus_list) {
  635. /* we don't really care if we can create this file or
  636. * not, but we need to assign the result of the call
  637. * or the world will fall under alien invasion and
  638. * everybody will be frozen on a spaceship ready to be
  639. * eaten on alpha centauri by some green and jelly
  640. * humanoid.
  641. */
  642. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  643. }
  644. list_for_each_entry(child_bus, &bus->children, node)
  645. pci_bus_register_of_sysfs(child_bus);
  646. }
  647. struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
  648. {
  649. struct device_node *node = pbm->prom_node;
  650. struct pci_bus *bus;
  651. printk("PCI: Scanning PBM %s\n", node->full_name);
  652. /* XXX parent device? XXX */
  653. bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
  654. if (!bus) {
  655. printk(KERN_ERR "Failed to create bus for %s\n",
  656. node->full_name);
  657. return NULL;
  658. }
  659. bus->secondary = pbm->pci_first_busno;
  660. bus->subordinate = pbm->pci_last_busno;
  661. bus->resource[0] = &pbm->io_space;
  662. bus->resource[1] = &pbm->mem_space;
  663. pci_of_scan_bus(pbm, node, bus);
  664. pci_bus_add_devices(bus);
  665. pci_bus_register_of_sysfs(bus);
  666. return bus;
  667. }
  668. static void __init pci_scan_each_controller_bus(void)
  669. {
  670. struct pci_pbm_info *pbm;
  671. for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
  672. if (pbm->scan_bus)
  673. pbm->scan_bus(pbm);
  674. }
  675. }
  676. static int __init pcibios_init(void)
  677. {
  678. pci_controller_probe();
  679. if (pci_pbm_root == NULL)
  680. return 0;
  681. pci_scan_each_controller_bus();
  682. return 0;
  683. }
  684. subsys_initcall(pcibios_init);
  685. void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
  686. {
  687. struct pci_pbm_info *pbm = pbus->sysdata;
  688. /* Generic PCI bus probing sets these to point at
  689. * &io{port,mem}_resouce which is wrong for us.
  690. */
  691. pbus->resource[0] = &pbm->io_space;
  692. pbus->resource[1] = &pbm->mem_space;
  693. }
  694. struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
  695. {
  696. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  697. struct resource *root = NULL;
  698. if (r->flags & IORESOURCE_IO)
  699. root = &pbm->io_space;
  700. if (r->flags & IORESOURCE_MEM)
  701. root = &pbm->mem_space;
  702. return root;
  703. }
  704. void pcibios_update_irq(struct pci_dev *pdev, int irq)
  705. {
  706. }
  707. void pcibios_align_resource(void *data, struct resource *res,
  708. resource_size_t size, resource_size_t align)
  709. {
  710. }
  711. int pcibios_enable_device(struct pci_dev *dev, int mask)
  712. {
  713. u16 cmd, oldcmd;
  714. int i;
  715. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  716. oldcmd = cmd;
  717. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  718. struct resource *res = &dev->resource[i];
  719. /* Only set up the requested stuff */
  720. if (!(mask & (1<<i)))
  721. continue;
  722. if (res->flags & IORESOURCE_IO)
  723. cmd |= PCI_COMMAND_IO;
  724. if (res->flags & IORESOURCE_MEM)
  725. cmd |= PCI_COMMAND_MEMORY;
  726. }
  727. if (cmd != oldcmd) {
  728. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  729. pci_name(dev), cmd);
  730. /* Enable the appropriate bits in the PCI command register. */
  731. pci_write_config_word(dev, PCI_COMMAND, cmd);
  732. }
  733. return 0;
  734. }
  735. void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
  736. struct resource *res)
  737. {
  738. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  739. struct resource zero_res, *root;
  740. zero_res.start = 0;
  741. zero_res.end = 0;
  742. zero_res.flags = res->flags;
  743. if (res->flags & IORESOURCE_IO)
  744. root = &pbm->io_space;
  745. else
  746. root = &pbm->mem_space;
  747. pci_resource_adjust(&zero_res, root);
  748. region->start = res->start - zero_res.start;
  749. region->end = res->end - zero_res.start;
  750. }
  751. EXPORT_SYMBOL(pcibios_resource_to_bus);
  752. void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
  753. struct pci_bus_region *region)
  754. {
  755. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  756. struct resource *root;
  757. res->start = region->start;
  758. res->end = region->end;
  759. if (res->flags & IORESOURCE_IO)
  760. root = &pbm->io_space;
  761. else
  762. root = &pbm->mem_space;
  763. pci_resource_adjust(res, root);
  764. }
  765. EXPORT_SYMBOL(pcibios_bus_to_resource);
  766. char * __devinit pcibios_setup(char *str)
  767. {
  768. return str;
  769. }
  770. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  771. /* If the user uses a host-bridge as the PCI device, he may use
  772. * this to perform a raw mmap() of the I/O or MEM space behind
  773. * that controller.
  774. *
  775. * This can be useful for execution of x86 PCI bios initialization code
  776. * on a PCI card, like the xfree86 int10 stuff does.
  777. */
  778. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  779. enum pci_mmap_state mmap_state)
  780. {
  781. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  782. unsigned long space_size, user_offset, user_size;
  783. if (mmap_state == pci_mmap_io) {
  784. space_size = (pbm->io_space.end -
  785. pbm->io_space.start) + 1;
  786. } else {
  787. space_size = (pbm->mem_space.end -
  788. pbm->mem_space.start) + 1;
  789. }
  790. /* Make sure the request is in range. */
  791. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  792. user_size = vma->vm_end - vma->vm_start;
  793. if (user_offset >= space_size ||
  794. (user_offset + user_size) > space_size)
  795. return -EINVAL;
  796. if (mmap_state == pci_mmap_io) {
  797. vma->vm_pgoff = (pbm->io_space.start +
  798. user_offset) >> PAGE_SHIFT;
  799. } else {
  800. vma->vm_pgoff = (pbm->mem_space.start +
  801. user_offset) >> PAGE_SHIFT;
  802. }
  803. return 0;
  804. }
  805. /* Adjust vm_pgoff of VMA such that it is the physical page offset
  806. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  807. *
  808. * Basically, the user finds the base address for his device which he wishes
  809. * to mmap. They read the 32-bit value from the config space base register,
  810. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  811. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  812. *
  813. * Returns negative error code on failure, zero on success.
  814. */
  815. static int __pci_mmap_make_offset(struct pci_dev *pdev,
  816. struct vm_area_struct *vma,
  817. enum pci_mmap_state mmap_state)
  818. {
  819. unsigned long user_paddr, user_size;
  820. int i, err;
  821. /* First compute the physical address in vma->vm_pgoff,
  822. * making sure the user offset is within range in the
  823. * appropriate PCI space.
  824. */
  825. err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
  826. if (err)
  827. return err;
  828. /* If this is a mapping on a host bridge, any address
  829. * is OK.
  830. */
  831. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  832. return err;
  833. /* Otherwise make sure it's in the range for one of the
  834. * device's resources.
  835. */
  836. user_paddr = vma->vm_pgoff << PAGE_SHIFT;
  837. user_size = vma->vm_end - vma->vm_start;
  838. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  839. struct resource *rp = &pdev->resource[i];
  840. /* Active? */
  841. if (!rp->flags)
  842. continue;
  843. /* Same type? */
  844. if (i == PCI_ROM_RESOURCE) {
  845. if (mmap_state != pci_mmap_mem)
  846. continue;
  847. } else {
  848. if ((mmap_state == pci_mmap_io &&
  849. (rp->flags & IORESOURCE_IO) == 0) ||
  850. (mmap_state == pci_mmap_mem &&
  851. (rp->flags & IORESOURCE_MEM) == 0))
  852. continue;
  853. }
  854. if ((rp->start <= user_paddr) &&
  855. (user_paddr + user_size) <= (rp->end + 1UL))
  856. break;
  857. }
  858. if (i > PCI_ROM_RESOURCE)
  859. return -EINVAL;
  860. return 0;
  861. }
  862. /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  863. * mapping.
  864. */
  865. static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  866. enum pci_mmap_state mmap_state)
  867. {
  868. vma->vm_flags |= (VM_IO | VM_RESERVED);
  869. }
  870. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  871. * device mapping.
  872. */
  873. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  874. enum pci_mmap_state mmap_state)
  875. {
  876. /* Our io_remap_pfn_range takes care of this, do nothing. */
  877. }
  878. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  879. * for this architecture. The region in the process to map is described by vm_start
  880. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  881. * The pci device structure is provided so that architectures may make mapping
  882. * decisions on a per-device or per-bus basis.
  883. *
  884. * Returns a negative error code on failure, zero on success.
  885. */
  886. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  887. enum pci_mmap_state mmap_state,
  888. int write_combine)
  889. {
  890. int ret;
  891. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  892. if (ret < 0)
  893. return ret;
  894. __pci_mmap_set_flags(dev, vma, mmap_state);
  895. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  896. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  897. ret = io_remap_pfn_range(vma, vma->vm_start,
  898. vma->vm_pgoff,
  899. vma->vm_end - vma->vm_start,
  900. vma->vm_page_prot);
  901. if (ret)
  902. return ret;
  903. return 0;
  904. }
  905. #ifdef CONFIG_NUMA
  906. int pcibus_to_node(struct pci_bus *pbus)
  907. {
  908. struct pci_pbm_info *pbm = pbus->sysdata;
  909. return pbm->numa_node;
  910. }
  911. EXPORT_SYMBOL(pcibus_to_node);
  912. #endif
  913. /* Return the domain nuber for this pci bus */
  914. int pci_domain_nr(struct pci_bus *pbus)
  915. {
  916. struct pci_pbm_info *pbm = pbus->sysdata;
  917. int ret;
  918. if (pbm == NULL || pbm->parent == NULL) {
  919. ret = -ENXIO;
  920. } else {
  921. ret = pbm->index;
  922. }
  923. return ret;
  924. }
  925. EXPORT_SYMBOL(pci_domain_nr);
  926. #ifdef CONFIG_PCI_MSI
  927. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  928. {
  929. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  930. int virt_irq;
  931. if (!pbm->setup_msi_irq)
  932. return -EINVAL;
  933. return pbm->setup_msi_irq(&virt_irq, pdev, desc);
  934. }
  935. void arch_teardown_msi_irq(unsigned int virt_irq)
  936. {
  937. struct msi_desc *entry = get_irq_msi(virt_irq);
  938. struct pci_dev *pdev = entry->dev;
  939. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  940. if (!pbm->teardown_msi_irq)
  941. return;
  942. return pbm->teardown_msi_irq(virt_irq, pdev);
  943. }
  944. #endif /* !(CONFIG_PCI_MSI) */
  945. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  946. {
  947. return pdev->dev.archdata.prom_node;
  948. }
  949. EXPORT_SYMBOL(pci_device_to_OF_node);
  950. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  951. {
  952. struct pci_dev *ali_isa_bridge;
  953. u8 val;
  954. /* ALI sound chips generate 31-bits of DMA, a special register
  955. * determines what bit 31 is emitted as.
  956. */
  957. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  958. PCI_DEVICE_ID_AL_M1533,
  959. NULL);
  960. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  961. if (set_bit)
  962. val |= 0x01;
  963. else
  964. val &= ~0x01;
  965. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  966. pci_dev_put(ali_isa_bridge);
  967. }
  968. int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
  969. {
  970. u64 dma_addr_mask;
  971. if (pdev == NULL) {
  972. dma_addr_mask = 0xffffffff;
  973. } else {
  974. struct iommu *iommu = pdev->dev.archdata.iommu;
  975. dma_addr_mask = iommu->dma_addr_mask;
  976. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  977. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  978. device_mask == 0x7fffffff) {
  979. ali_sound_dma_hack(pdev,
  980. (dma_addr_mask & 0x80000000) != 0);
  981. return 1;
  982. }
  983. }
  984. if (device_mask >= (1UL << 32UL))
  985. return 0;
  986. return (device_mask & dma_addr_mask) == dma_addr_mask;
  987. }
  988. void pci_resource_to_user(const struct pci_dev *pdev, int bar,
  989. const struct resource *rp, resource_size_t *start,
  990. resource_size_t *end)
  991. {
  992. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  993. unsigned long offset;
  994. if (rp->flags & IORESOURCE_IO)
  995. offset = pbm->io_space.start;
  996. else
  997. offset = pbm->mem_space.start;
  998. *start = rp->start - offset;
  999. *end = rp->end - offset;
  1000. }