pci.c 24 KB

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