pci.c 28 KB

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