pci.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. 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 of_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. } else {
  206. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  207. continue;
  208. }
  209. res->start = op_res->start;
  210. res->end = op_res->end;
  211. res->flags = flags;
  212. res->name = pci_name(dev);
  213. }
  214. }
  215. static struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  216. struct device_node *node,
  217. struct pci_bus *bus, int devfn)
  218. {
  219. struct dev_archdata *sd;
  220. struct pci_slot *slot;
  221. struct of_device *op;
  222. struct pci_dev *dev;
  223. const char *type;
  224. u32 class;
  225. dev = alloc_pci_dev();
  226. if (!dev)
  227. return NULL;
  228. sd = &dev->dev.archdata;
  229. sd->iommu = pbm->iommu;
  230. sd->stc = &pbm->stc;
  231. sd->host_controller = pbm;
  232. sd->prom_node = node;
  233. dev->dev.of_node = node;
  234. sd->op = op = of_find_device_by_node(node);
  235. sd->numa_node = pbm->numa_node;
  236. sd = &op->dev.archdata;
  237. sd->iommu = pbm->iommu;
  238. sd->stc = &pbm->stc;
  239. sd->numa_node = pbm->numa_node;
  240. if (!strcmp(node->name, "ebus"))
  241. of_propagate_archdata(op);
  242. type = of_get_property(node, "device_type", NULL);
  243. if (type == NULL)
  244. type = "";
  245. if (ofpci_verbose)
  246. printk(" create device, devfn: %x, type: %s\n",
  247. devfn, type);
  248. dev->bus = bus;
  249. dev->sysdata = node;
  250. dev->dev.parent = bus->bridge;
  251. dev->dev.bus = &pci_bus_type;
  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->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. static void pci_resource_adjust(struct resource *res,
  324. struct resource *root)
  325. {
  326. res->start += root->start;
  327. res->end += root->start;
  328. }
  329. /* For PCI bus devices which lack a 'ranges' property we interrogate
  330. * the config space values to set the resources, just like the generic
  331. * Linux PCI probing code does.
  332. */
  333. static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
  334. struct pci_bus *bus,
  335. struct pci_pbm_info *pbm)
  336. {
  337. struct resource *res;
  338. u8 io_base_lo, io_limit_lo;
  339. u16 mem_base_lo, mem_limit_lo;
  340. unsigned long base, limit;
  341. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  342. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  343. base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
  344. limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
  345. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  346. u16 io_base_hi, io_limit_hi;
  347. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  348. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  349. base |= (io_base_hi << 16);
  350. limit |= (io_limit_hi << 16);
  351. }
  352. res = bus->resource[0];
  353. if (base <= limit) {
  354. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  355. if (!res->start)
  356. res->start = base;
  357. if (!res->end)
  358. res->end = limit + 0xfff;
  359. pci_resource_adjust(res, &pbm->io_space);
  360. }
  361. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  362. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  363. base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  364. limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  365. res = bus->resource[1];
  366. if (base <= limit) {
  367. res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
  368. IORESOURCE_MEM);
  369. res->start = base;
  370. res->end = limit + 0xfffff;
  371. pci_resource_adjust(res, &pbm->mem_space);
  372. }
  373. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  374. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  375. base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  376. limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  377. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  378. u32 mem_base_hi, mem_limit_hi;
  379. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  380. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  381. /*
  382. * Some bridges set the base > limit by default, and some
  383. * (broken) BIOSes do not initialize them. If we find
  384. * this, just assume they are not being used.
  385. */
  386. if (mem_base_hi <= mem_limit_hi) {
  387. base |= ((long) mem_base_hi) << 32;
  388. limit |= ((long) mem_limit_hi) << 32;
  389. }
  390. }
  391. res = bus->resource[2];
  392. if (base <= limit) {
  393. res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
  394. IORESOURCE_MEM | IORESOURCE_PREFETCH);
  395. res->start = base;
  396. res->end = limit + 0xfffff;
  397. pci_resource_adjust(res, &pbm->mem_space);
  398. }
  399. }
  400. /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  401. * a proper 'ranges' property.
  402. */
  403. static void __devinit apb_fake_ranges(struct pci_dev *dev,
  404. struct pci_bus *bus,
  405. struct pci_pbm_info *pbm)
  406. {
  407. struct resource *res;
  408. u32 first, last;
  409. u8 map;
  410. pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
  411. apb_calc_first_last(map, &first, &last);
  412. res = bus->resource[0];
  413. res->start = (first << 21);
  414. res->end = (last << 21) + ((1 << 21) - 1);
  415. res->flags = IORESOURCE_IO;
  416. pci_resource_adjust(res, &pbm->io_space);
  417. pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
  418. apb_calc_first_last(map, &first, &last);
  419. res = bus->resource[1];
  420. res->start = (first << 21);
  421. res->end = (last << 21) + ((1 << 21) - 1);
  422. res->flags = IORESOURCE_MEM;
  423. pci_resource_adjust(res, &pbm->mem_space);
  424. }
  425. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  426. struct device_node *node,
  427. struct pci_bus *bus);
  428. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  429. static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
  430. struct device_node *node,
  431. struct pci_dev *dev)
  432. {
  433. struct pci_bus *bus;
  434. const u32 *busrange, *ranges;
  435. int len, i, simba;
  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. struct resource *root;
  482. flags = pci_parse_of_flags(ranges[0]);
  483. size = GET_64BIT(ranges, 6);
  484. if (flags == 0 || size == 0)
  485. continue;
  486. if (flags & IORESOURCE_IO) {
  487. res = bus->resource[0];
  488. if (res->flags) {
  489. printk(KERN_ERR "PCI: ignoring extra I/O range"
  490. " for bridge %s\n", node->full_name);
  491. continue;
  492. }
  493. root = &pbm->io_space;
  494. } else {
  495. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  496. printk(KERN_ERR "PCI: too many memory ranges"
  497. " for bridge %s\n", node->full_name);
  498. continue;
  499. }
  500. res = bus->resource[i];
  501. ++i;
  502. root = &pbm->mem_space;
  503. }
  504. res->start = GET_64BIT(ranges, 1);
  505. res->end = res->start + size - 1;
  506. res->flags = flags;
  507. /* Another way to implement this would be to add an of_device
  508. * layer routine that can calculate a resource for a given
  509. * range property value in a PCI device.
  510. */
  511. pci_resource_adjust(res, root);
  512. }
  513. after_ranges:
  514. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  515. bus->number);
  516. if (ofpci_verbose)
  517. printk(" bus name: %s\n", bus->name);
  518. pci_of_scan_bus(pbm, node, bus);
  519. }
  520. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  521. struct device_node *node,
  522. struct pci_bus *bus)
  523. {
  524. struct device_node *child;
  525. const u32 *reg;
  526. int reglen, devfn, prev_devfn;
  527. struct pci_dev *dev;
  528. if (ofpci_verbose)
  529. printk("PCI: scan_bus[%s] bus no %d\n",
  530. node->full_name, bus->number);
  531. child = NULL;
  532. prev_devfn = -1;
  533. while ((child = of_get_next_child(node, child)) != NULL) {
  534. if (ofpci_verbose)
  535. printk(" * %s\n", child->full_name);
  536. reg = of_get_property(child, "reg", &reglen);
  537. if (reg == NULL || reglen < 20)
  538. continue;
  539. devfn = (reg[0] >> 8) & 0xff;
  540. /* This is a workaround for some device trees
  541. * which list PCI devices twice. On the V100
  542. * for example, device number 3 is listed twice.
  543. * Once as "pm" and once again as "lomp".
  544. */
  545. if (devfn == prev_devfn)
  546. continue;
  547. prev_devfn = devfn;
  548. /* create a new pci_dev for this device */
  549. dev = of_create_pci_dev(pbm, child, bus, devfn);
  550. if (!dev)
  551. continue;
  552. if (ofpci_verbose)
  553. printk("PCI: dev header type: %x\n",
  554. dev->hdr_type);
  555. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  556. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  557. of_scan_pci_bridge(pbm, child, dev);
  558. }
  559. }
  560. static ssize_t
  561. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  562. {
  563. struct pci_dev *pdev;
  564. struct device_node *dp;
  565. pdev = to_pci_dev(dev);
  566. dp = pdev->dev.archdata.prom_node;
  567. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  568. }
  569. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  570. static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
  571. {
  572. struct pci_dev *dev;
  573. struct pci_bus *child_bus;
  574. int err;
  575. list_for_each_entry(dev, &bus->devices, bus_list) {
  576. /* we don't really care if we can create this file or
  577. * not, but we need to assign the result of the call
  578. * or the world will fall under alien invasion and
  579. * everybody will be frozen on a spaceship ready to be
  580. * eaten on alpha centauri by some green and jelly
  581. * humanoid.
  582. */
  583. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  584. }
  585. list_for_each_entry(child_bus, &bus->children, node)
  586. pci_bus_register_of_sysfs(child_bus);
  587. }
  588. struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm,
  589. struct device *parent)
  590. {
  591. struct device_node *node = pbm->op->node;
  592. struct pci_bus *bus;
  593. printk("PCI: Scanning PBM %s\n", node->full_name);
  594. bus = pci_create_bus(parent, pbm->pci_first_busno, pbm->pci_ops, pbm);
  595. if (!bus) {
  596. printk(KERN_ERR "Failed to create bus for %s\n",
  597. node->full_name);
  598. return NULL;
  599. }
  600. bus->secondary = pbm->pci_first_busno;
  601. bus->subordinate = pbm->pci_last_busno;
  602. bus->resource[0] = &pbm->io_space;
  603. bus->resource[1] = &pbm->mem_space;
  604. pci_of_scan_bus(pbm, node, bus);
  605. pci_bus_add_devices(bus);
  606. pci_bus_register_of_sysfs(bus);
  607. return bus;
  608. }
  609. void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
  610. {
  611. struct pci_pbm_info *pbm = pbus->sysdata;
  612. /* Generic PCI bus probing sets these to point at
  613. * &io{port,mem}_resouce which is wrong for us.
  614. */
  615. pbus->resource[0] = &pbm->io_space;
  616. pbus->resource[1] = &pbm->mem_space;
  617. }
  618. void pcibios_update_irq(struct pci_dev *pdev, int irq)
  619. {
  620. }
  621. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  622. resource_size_t size, resource_size_t align)
  623. {
  624. return res->start;
  625. }
  626. int pcibios_enable_device(struct pci_dev *dev, int mask)
  627. {
  628. u16 cmd, oldcmd;
  629. int i;
  630. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  631. oldcmd = cmd;
  632. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  633. struct resource *res = &dev->resource[i];
  634. /* Only set up the requested stuff */
  635. if (!(mask & (1<<i)))
  636. continue;
  637. if (res->flags & IORESOURCE_IO)
  638. cmd |= PCI_COMMAND_IO;
  639. if (res->flags & IORESOURCE_MEM)
  640. cmd |= PCI_COMMAND_MEMORY;
  641. }
  642. if (cmd != oldcmd) {
  643. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  644. pci_name(dev), cmd);
  645. /* Enable the appropriate bits in the PCI command register. */
  646. pci_write_config_word(dev, PCI_COMMAND, cmd);
  647. }
  648. return 0;
  649. }
  650. void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
  651. struct resource *res)
  652. {
  653. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  654. struct resource zero_res, *root;
  655. zero_res.start = 0;
  656. zero_res.end = 0;
  657. zero_res.flags = res->flags;
  658. if (res->flags & IORESOURCE_IO)
  659. root = &pbm->io_space;
  660. else
  661. root = &pbm->mem_space;
  662. pci_resource_adjust(&zero_res, root);
  663. region->start = res->start - zero_res.start;
  664. region->end = res->end - zero_res.start;
  665. }
  666. EXPORT_SYMBOL(pcibios_resource_to_bus);
  667. void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
  668. struct pci_bus_region *region)
  669. {
  670. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  671. struct resource *root;
  672. res->start = region->start;
  673. res->end = region->end;
  674. if (res->flags & IORESOURCE_IO)
  675. root = &pbm->io_space;
  676. else
  677. root = &pbm->mem_space;
  678. pci_resource_adjust(res, root);
  679. }
  680. EXPORT_SYMBOL(pcibios_bus_to_resource);
  681. char * __devinit pcibios_setup(char *str)
  682. {
  683. return str;
  684. }
  685. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  686. /* If the user uses a host-bridge as the PCI device, he may use
  687. * this to perform a raw mmap() of the I/O or MEM space behind
  688. * that controller.
  689. *
  690. * This can be useful for execution of x86 PCI bios initialization code
  691. * on a PCI card, like the xfree86 int10 stuff does.
  692. */
  693. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  694. enum pci_mmap_state mmap_state)
  695. {
  696. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  697. unsigned long space_size, user_offset, user_size;
  698. if (mmap_state == pci_mmap_io) {
  699. space_size = (pbm->io_space.end -
  700. pbm->io_space.start) + 1;
  701. } else {
  702. space_size = (pbm->mem_space.end -
  703. pbm->mem_space.start) + 1;
  704. }
  705. /* Make sure the request is in range. */
  706. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  707. user_size = vma->vm_end - vma->vm_start;
  708. if (user_offset >= space_size ||
  709. (user_offset + user_size) > space_size)
  710. return -EINVAL;
  711. if (mmap_state == pci_mmap_io) {
  712. vma->vm_pgoff = (pbm->io_space.start +
  713. user_offset) >> PAGE_SHIFT;
  714. } else {
  715. vma->vm_pgoff = (pbm->mem_space.start +
  716. user_offset) >> PAGE_SHIFT;
  717. }
  718. return 0;
  719. }
  720. /* Adjust vm_pgoff of VMA such that it is the physical page offset
  721. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  722. *
  723. * Basically, the user finds the base address for his device which he wishes
  724. * to mmap. They read the 32-bit value from the config space base register,
  725. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  726. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  727. *
  728. * Returns negative error code on failure, zero on success.
  729. */
  730. static int __pci_mmap_make_offset(struct pci_dev *pdev,
  731. struct vm_area_struct *vma,
  732. enum pci_mmap_state mmap_state)
  733. {
  734. unsigned long user_paddr, user_size;
  735. int i, err;
  736. /* First compute the physical address in vma->vm_pgoff,
  737. * making sure the user offset is within range in the
  738. * appropriate PCI space.
  739. */
  740. err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
  741. if (err)
  742. return err;
  743. /* If this is a mapping on a host bridge, any address
  744. * is OK.
  745. */
  746. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  747. return err;
  748. /* Otherwise make sure it's in the range for one of the
  749. * device's resources.
  750. */
  751. user_paddr = vma->vm_pgoff << PAGE_SHIFT;
  752. user_size = vma->vm_end - vma->vm_start;
  753. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  754. struct resource *rp = &pdev->resource[i];
  755. resource_size_t aligned_end;
  756. /* Active? */
  757. if (!rp->flags)
  758. continue;
  759. /* Same type? */
  760. if (i == PCI_ROM_RESOURCE) {
  761. if (mmap_state != pci_mmap_mem)
  762. continue;
  763. } else {
  764. if ((mmap_state == pci_mmap_io &&
  765. (rp->flags & IORESOURCE_IO) == 0) ||
  766. (mmap_state == pci_mmap_mem &&
  767. (rp->flags & IORESOURCE_MEM) == 0))
  768. continue;
  769. }
  770. /* Align the resource end to the next page address.
  771. * PAGE_SIZE intentionally added instead of (PAGE_SIZE - 1),
  772. * because actually we need the address of the next byte
  773. * after rp->end.
  774. */
  775. aligned_end = (rp->end + PAGE_SIZE) & PAGE_MASK;
  776. if ((rp->start <= user_paddr) &&
  777. (user_paddr + user_size) <= aligned_end)
  778. break;
  779. }
  780. if (i > PCI_ROM_RESOURCE)
  781. return -EINVAL;
  782. return 0;
  783. }
  784. /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  785. * mapping.
  786. */
  787. static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  788. enum pci_mmap_state mmap_state)
  789. {
  790. vma->vm_flags |= (VM_IO | VM_RESERVED);
  791. }
  792. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  793. * device mapping.
  794. */
  795. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  796. enum pci_mmap_state mmap_state)
  797. {
  798. /* Our io_remap_pfn_range takes care of this, do nothing. */
  799. }
  800. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  801. * for this architecture. The region in the process to map is described by vm_start
  802. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  803. * The pci device structure is provided so that architectures may make mapping
  804. * decisions on a per-device or per-bus basis.
  805. *
  806. * Returns a negative error code on failure, zero on success.
  807. */
  808. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  809. enum pci_mmap_state mmap_state,
  810. int write_combine)
  811. {
  812. int ret;
  813. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  814. if (ret < 0)
  815. return ret;
  816. __pci_mmap_set_flags(dev, vma, mmap_state);
  817. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  818. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  819. ret = io_remap_pfn_range(vma, vma->vm_start,
  820. vma->vm_pgoff,
  821. vma->vm_end - vma->vm_start,
  822. vma->vm_page_prot);
  823. if (ret)
  824. return ret;
  825. return 0;
  826. }
  827. #ifdef CONFIG_NUMA
  828. int pcibus_to_node(struct pci_bus *pbus)
  829. {
  830. struct pci_pbm_info *pbm = pbus->sysdata;
  831. return pbm->numa_node;
  832. }
  833. EXPORT_SYMBOL(pcibus_to_node);
  834. #endif
  835. /* Return the domain number for this pci bus */
  836. int pci_domain_nr(struct pci_bus *pbus)
  837. {
  838. struct pci_pbm_info *pbm = pbus->sysdata;
  839. int ret;
  840. if (!pbm) {
  841. ret = -ENXIO;
  842. } else {
  843. ret = pbm->index;
  844. }
  845. return ret;
  846. }
  847. EXPORT_SYMBOL(pci_domain_nr);
  848. #ifdef CONFIG_PCI_MSI
  849. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  850. {
  851. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  852. unsigned int virt_irq;
  853. if (!pbm->setup_msi_irq)
  854. return -EINVAL;
  855. return pbm->setup_msi_irq(&virt_irq, pdev, desc);
  856. }
  857. void arch_teardown_msi_irq(unsigned int virt_irq)
  858. {
  859. struct msi_desc *entry = get_irq_msi(virt_irq);
  860. struct pci_dev *pdev = entry->dev;
  861. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  862. if (pbm->teardown_msi_irq)
  863. pbm->teardown_msi_irq(virt_irq, pdev);
  864. }
  865. #endif /* !(CONFIG_PCI_MSI) */
  866. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  867. {
  868. return pdev->dev.archdata.prom_node;
  869. }
  870. EXPORT_SYMBOL(pci_device_to_OF_node);
  871. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  872. {
  873. struct pci_dev *ali_isa_bridge;
  874. u8 val;
  875. /* ALI sound chips generate 31-bits of DMA, a special register
  876. * determines what bit 31 is emitted as.
  877. */
  878. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  879. PCI_DEVICE_ID_AL_M1533,
  880. NULL);
  881. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  882. if (set_bit)
  883. val |= 0x01;
  884. else
  885. val &= ~0x01;
  886. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  887. pci_dev_put(ali_isa_bridge);
  888. }
  889. int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask)
  890. {
  891. u64 dma_addr_mask;
  892. if (pdev == NULL) {
  893. dma_addr_mask = 0xffffffff;
  894. } else {
  895. struct iommu *iommu = pdev->dev.archdata.iommu;
  896. dma_addr_mask = iommu->dma_addr_mask;
  897. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  898. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  899. device_mask == 0x7fffffff) {
  900. ali_sound_dma_hack(pdev,
  901. (dma_addr_mask & 0x80000000) != 0);
  902. return 1;
  903. }
  904. }
  905. if (device_mask >= (1UL << 32UL))
  906. return 0;
  907. return (device_mask & dma_addr_mask) == dma_addr_mask;
  908. }
  909. void pci_resource_to_user(const struct pci_dev *pdev, int bar,
  910. const struct resource *rp, resource_size_t *start,
  911. resource_size_t *end)
  912. {
  913. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  914. unsigned long offset;
  915. if (rp->flags & IORESOURCE_IO)
  916. offset = pbm->io_space.start;
  917. else
  918. offset = pbm->mem_space.start;
  919. *start = rp->start - offset;
  920. *end = rp->end - offset;
  921. }
  922. static int __init pcibios_init(void)
  923. {
  924. pci_dfl_cache_line_size = 64 >> 2;
  925. return 0;
  926. }
  927. subsys_initcall(pcibios_init);
  928. #ifdef CONFIG_SYSFS
  929. static void __devinit pci_bus_slot_names(struct device_node *node,
  930. struct pci_bus *bus)
  931. {
  932. const struct pci_slot_names {
  933. u32 slot_mask;
  934. char names[0];
  935. } *prop;
  936. const char *sp;
  937. int len, i;
  938. u32 mask;
  939. prop = of_get_property(node, "slot-names", &len);
  940. if (!prop)
  941. return;
  942. mask = prop->slot_mask;
  943. sp = prop->names;
  944. if (ofpci_verbose)
  945. printk("PCI: Making slots for [%s] mask[0x%02x]\n",
  946. node->full_name, mask);
  947. i = 0;
  948. while (mask) {
  949. struct pci_slot *pci_slot;
  950. u32 this_bit = 1 << i;
  951. if (!(mask & this_bit)) {
  952. i++;
  953. continue;
  954. }
  955. if (ofpci_verbose)
  956. printk("PCI: Making slot [%s]\n", sp);
  957. pci_slot = pci_create_slot(bus, i, sp, NULL);
  958. if (IS_ERR(pci_slot))
  959. printk(KERN_ERR "PCI: pci_create_slot returned %ld\n",
  960. PTR_ERR(pci_slot));
  961. sp += strlen(sp) + 1;
  962. mask &= ~this_bit;
  963. i++;
  964. }
  965. }
  966. static int __init of_pci_slot_init(void)
  967. {
  968. struct pci_bus *pbus = NULL;
  969. while ((pbus = pci_find_next_bus(pbus)) != NULL) {
  970. struct device_node *node;
  971. if (pbus->self) {
  972. struct dev_archdata *sd = pbus->self->sysdata;
  973. /* PCI->PCI bridge */
  974. node = sd->prom_node;
  975. } else {
  976. struct pci_pbm_info *pbm = pbus->sysdata;
  977. /* Host PCI controller */
  978. node = pbm->op->node;
  979. }
  980. pci_bus_slot_names(node, pbus);
  981. }
  982. return 0;
  983. }
  984. module_init(of_pci_slot_init);
  985. #endif