pci.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. *
  7. * The System z PCI code is a rewrite from a prototype by
  8. * the following people (Kudoz!):
  9. * Alexander Schmidt
  10. * Christoph Raisch
  11. * Hannes Hering
  12. * Hoang-Nam Nguyen
  13. * Jan-Bernd Themann
  14. * Stefan Roscher
  15. * Thomas Klein
  16. */
  17. #define COMPONENT "zPCI"
  18. #define pr_fmt(fmt) COMPONENT ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/export.h>
  23. #include <linux/delay.h>
  24. #include <linux/irq.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/pci.h>
  28. #include <linux/msi.h>
  29. #include <asm/isc.h>
  30. #include <asm/airq.h>
  31. #include <asm/facility.h>
  32. #include <asm/pci_insn.h>
  33. #include <asm/pci_clp.h>
  34. #include <asm/pci_dma.h>
  35. #define DEBUG /* enable pr_debug */
  36. #define SIC_IRQ_MODE_ALL 0
  37. #define SIC_IRQ_MODE_SINGLE 1
  38. #define ZPCI_NR_DMA_SPACES 1
  39. #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
  40. /* list of all detected zpci devices */
  41. static LIST_HEAD(zpci_list);
  42. static DEFINE_SPINLOCK(zpci_list_lock);
  43. static void zpci_enable_irq(struct irq_data *data);
  44. static void zpci_disable_irq(struct irq_data *data);
  45. static struct irq_chip zpci_irq_chip = {
  46. .name = "zPCI",
  47. .irq_unmask = zpci_enable_irq,
  48. .irq_mask = zpci_disable_irq,
  49. };
  50. static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
  51. static DEFINE_SPINLOCK(zpci_domain_lock);
  52. static struct airq_iv *zpci_aisb_iv;
  53. static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES];
  54. /* Adapter interrupt definitions */
  55. static void zpci_irq_handler(struct airq_struct *airq);
  56. static struct airq_struct zpci_airq = {
  57. .handler = zpci_irq_handler,
  58. .isc = PCI_ISC,
  59. };
  60. /* I/O Map */
  61. static DEFINE_SPINLOCK(zpci_iomap_lock);
  62. static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  63. struct zpci_iomap_entry *zpci_iomap_start;
  64. EXPORT_SYMBOL_GPL(zpci_iomap_start);
  65. static struct kmem_cache *zdev_fmb_cache;
  66. struct zpci_dev *get_zdev(struct pci_dev *pdev)
  67. {
  68. return (struct zpci_dev *) pdev->sysdata;
  69. }
  70. struct zpci_dev *get_zdev_by_fid(u32 fid)
  71. {
  72. struct zpci_dev *tmp, *zdev = NULL;
  73. spin_lock(&zpci_list_lock);
  74. list_for_each_entry(tmp, &zpci_list, entry) {
  75. if (tmp->fid == fid) {
  76. zdev = tmp;
  77. break;
  78. }
  79. }
  80. spin_unlock(&zpci_list_lock);
  81. return zdev;
  82. }
  83. static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
  84. {
  85. return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
  86. }
  87. int pci_domain_nr(struct pci_bus *bus)
  88. {
  89. return ((struct zpci_dev *) bus->sysdata)->domain;
  90. }
  91. EXPORT_SYMBOL_GPL(pci_domain_nr);
  92. int pci_proc_domain(struct pci_bus *bus)
  93. {
  94. return pci_domain_nr(bus);
  95. }
  96. EXPORT_SYMBOL_GPL(pci_proc_domain);
  97. /* Modify PCI: Register adapter interruptions */
  98. static int zpci_set_airq(struct zpci_dev *zdev)
  99. {
  100. u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
  101. struct zpci_fib fib = {0};
  102. fib.isc = PCI_ISC;
  103. fib.sum = 1; /* enable summary notifications */
  104. fib.noi = airq_iv_end(zdev->aibv);
  105. fib.aibv = (unsigned long) zdev->aibv->vector;
  106. fib.aibvo = 0; /* each zdev has its own interrupt vector */
  107. fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8;
  108. fib.aisbo = zdev->aisb & 63;
  109. return zpci_mod_fc(req, &fib);
  110. }
  111. struct mod_pci_args {
  112. u64 base;
  113. u64 limit;
  114. u64 iota;
  115. u64 fmb_addr;
  116. };
  117. static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
  118. {
  119. u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
  120. struct zpci_fib fib = {0};
  121. fib.pba = args->base;
  122. fib.pal = args->limit;
  123. fib.iota = args->iota;
  124. fib.fmb_addr = args->fmb_addr;
  125. return zpci_mod_fc(req, &fib);
  126. }
  127. /* Modify PCI: Register I/O address translation parameters */
  128. int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
  129. u64 base, u64 limit, u64 iota)
  130. {
  131. struct mod_pci_args args = { base, limit, iota, 0 };
  132. WARN_ON_ONCE(iota & 0x3fff);
  133. args.iota |= ZPCI_IOTA_RTTO_FLAG;
  134. return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
  135. }
  136. /* Modify PCI: Unregister I/O address translation parameters */
  137. int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
  138. {
  139. struct mod_pci_args args = { 0, 0, 0, 0 };
  140. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
  141. }
  142. /* Modify PCI: Unregister adapter interruptions */
  143. static int zpci_clear_airq(struct zpci_dev *zdev)
  144. {
  145. struct mod_pci_args args = { 0, 0, 0, 0 };
  146. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
  147. }
  148. /* Modify PCI: Set PCI function measurement parameters */
  149. int zpci_fmb_enable_device(struct zpci_dev *zdev)
  150. {
  151. struct mod_pci_args args = { 0, 0, 0, 0 };
  152. if (zdev->fmb)
  153. return -EINVAL;
  154. zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
  155. if (!zdev->fmb)
  156. return -ENOMEM;
  157. WARN_ON((u64) zdev->fmb & 0xf);
  158. args.fmb_addr = virt_to_phys(zdev->fmb);
  159. return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  160. }
  161. /* Modify PCI: Disable PCI function measurement */
  162. int zpci_fmb_disable_device(struct zpci_dev *zdev)
  163. {
  164. struct mod_pci_args args = { 0, 0, 0, 0 };
  165. int rc;
  166. if (!zdev->fmb)
  167. return -EINVAL;
  168. /* Function measurement is disabled if fmb address is zero */
  169. rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  170. kmem_cache_free(zdev_fmb_cache, zdev->fmb);
  171. zdev->fmb = NULL;
  172. return rc;
  173. }
  174. #define ZPCI_PCIAS_CFGSPC 15
  175. static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
  176. {
  177. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  178. u64 data;
  179. int rc;
  180. rc = zpci_load(&data, req, offset);
  181. if (!rc) {
  182. data = data << ((8 - len) * 8);
  183. data = le64_to_cpu(data);
  184. *val = (u32) data;
  185. } else
  186. *val = 0xffffffff;
  187. return rc;
  188. }
  189. static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
  190. {
  191. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  192. u64 data = val;
  193. int rc;
  194. data = cpu_to_le64(data);
  195. data = data >> ((8 - len) * 8);
  196. rc = zpci_store(data, req, offset);
  197. return rc;
  198. }
  199. static int zpci_msi_set_mask_bits(struct msi_desc *msi, u32 mask, u32 flag)
  200. {
  201. int offset, pos;
  202. u32 mask_bits;
  203. if (msi->msi_attrib.is_msix) {
  204. offset = msi->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  205. PCI_MSIX_ENTRY_VECTOR_CTRL;
  206. msi->masked = readl(msi->mask_base + offset);
  207. writel(flag, msi->mask_base + offset);
  208. } else if (msi->msi_attrib.maskbit) {
  209. pos = (long) msi->mask_base;
  210. pci_read_config_dword(msi->dev, pos, &mask_bits);
  211. mask_bits &= ~(mask);
  212. mask_bits |= flag & mask;
  213. pci_write_config_dword(msi->dev, pos, mask_bits);
  214. } else
  215. return 0;
  216. msi->msi_attrib.maskbit = !!flag;
  217. return 1;
  218. }
  219. static void zpci_enable_irq(struct irq_data *data)
  220. {
  221. struct msi_desc *msi = irq_get_msi_desc(data->irq);
  222. zpci_msi_set_mask_bits(msi, 1, 0);
  223. }
  224. static void zpci_disable_irq(struct irq_data *data)
  225. {
  226. struct msi_desc *msi = irq_get_msi_desc(data->irq);
  227. zpci_msi_set_mask_bits(msi, 1, 1);
  228. }
  229. void pcibios_fixup_bus(struct pci_bus *bus)
  230. {
  231. }
  232. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  233. resource_size_t size,
  234. resource_size_t align)
  235. {
  236. return 0;
  237. }
  238. /* combine single writes by using store-block insn */
  239. void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
  240. {
  241. zpci_memcpy_toio(to, from, count);
  242. }
  243. /* Create a virtual mapping cookie for a PCI BAR */
  244. void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
  245. {
  246. struct zpci_dev *zdev = get_zdev(pdev);
  247. u64 addr;
  248. int idx;
  249. if ((bar & 7) != bar)
  250. return NULL;
  251. idx = zdev->bars[bar].map_idx;
  252. spin_lock(&zpci_iomap_lock);
  253. zpci_iomap_start[idx].fh = zdev->fh;
  254. zpci_iomap_start[idx].bar = bar;
  255. spin_unlock(&zpci_iomap_lock);
  256. addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
  257. return (void __iomem *) addr;
  258. }
  259. EXPORT_SYMBOL_GPL(pci_iomap);
  260. void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
  261. {
  262. unsigned int idx;
  263. idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
  264. spin_lock(&zpci_iomap_lock);
  265. zpci_iomap_start[idx].fh = 0;
  266. zpci_iomap_start[idx].bar = 0;
  267. spin_unlock(&zpci_iomap_lock);
  268. }
  269. EXPORT_SYMBOL_GPL(pci_iounmap);
  270. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  271. int size, u32 *val)
  272. {
  273. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  274. int ret;
  275. if (!zdev || devfn != ZPCI_DEVFN)
  276. ret = -ENODEV;
  277. else
  278. ret = zpci_cfg_load(zdev, where, val, size);
  279. return ret;
  280. }
  281. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  282. int size, u32 val)
  283. {
  284. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  285. int ret;
  286. if (!zdev || devfn != ZPCI_DEVFN)
  287. ret = -ENODEV;
  288. else
  289. ret = zpci_cfg_store(zdev, where, val, size);
  290. return ret;
  291. }
  292. static struct pci_ops pci_root_ops = {
  293. .read = pci_read,
  294. .write = pci_write,
  295. };
  296. static void zpci_irq_handler(struct airq_struct *airq)
  297. {
  298. unsigned long si, ai;
  299. struct airq_iv *aibv;
  300. int irqs_on = 0;
  301. inc_irq_stat(IRQIO_PCI);
  302. for (si = 0;;) {
  303. /* Scan adapter summary indicator bit vector */
  304. si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv));
  305. if (si == -1UL) {
  306. if (irqs_on++)
  307. /* End of second scan with interrupts on. */
  308. break;
  309. /* First scan complete, reenable interrupts. */
  310. zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  311. si = 0;
  312. continue;
  313. }
  314. /* Scan the adapter interrupt vector for this device. */
  315. aibv = zpci_aibv[si];
  316. for (ai = 0;;) {
  317. ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv));
  318. if (ai == -1UL)
  319. break;
  320. inc_irq_stat(IRQIO_MSI);
  321. airq_iv_lock(aibv, ai);
  322. generic_handle_irq(airq_iv_get_data(aibv, ai));
  323. airq_iv_unlock(aibv, ai);
  324. }
  325. }
  326. }
  327. int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  328. {
  329. struct zpci_dev *zdev = get_zdev(pdev);
  330. unsigned int hwirq, irq, msi_vecs;
  331. unsigned long aisb;
  332. struct msi_desc *msi;
  333. struct msi_msg msg;
  334. int rc;
  335. if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI)
  336. return -EINVAL;
  337. msi_vecs = min(nvec, ZPCI_MSI_VEC_MAX);
  338. msi_vecs = min_t(unsigned int, msi_vecs, CONFIG_PCI_NR_MSI);
  339. /* Allocate adapter summary indicator bit */
  340. rc = -EIO;
  341. aisb = airq_iv_alloc_bit(zpci_aisb_iv);
  342. if (aisb == -1UL)
  343. goto out;
  344. zdev->aisb = aisb;
  345. /* Create adapter interrupt vector */
  346. rc = -ENOMEM;
  347. zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK);
  348. if (!zdev->aibv)
  349. goto out_si;
  350. /* Wire up shortcut pointer */
  351. zpci_aibv[aisb] = zdev->aibv;
  352. /* Request MSI interrupts */
  353. hwirq = 0;
  354. list_for_each_entry(msi, &pdev->msi_list, list) {
  355. rc = -EIO;
  356. irq = irq_alloc_desc(0); /* Alloc irq on node 0 */
  357. if (irq == NO_IRQ)
  358. goto out_msi;
  359. rc = irq_set_msi_desc(irq, msi);
  360. if (rc)
  361. goto out_msi;
  362. irq_set_chip_and_handler(irq, &zpci_irq_chip,
  363. handle_simple_irq);
  364. msg.data = hwirq;
  365. msg.address_lo = zdev->msi_addr & 0xffffffff;
  366. msg.address_hi = zdev->msi_addr >> 32;
  367. write_msi_msg(irq, &msg);
  368. airq_iv_set_data(zdev->aibv, hwirq, irq);
  369. hwirq++;
  370. }
  371. /* Enable adapter interrupts */
  372. rc = zpci_set_airq(zdev);
  373. if (rc)
  374. goto out_msi;
  375. return (msi_vecs == nvec) ? 0 : msi_vecs;
  376. out_msi:
  377. list_for_each_entry(msi, &pdev->msi_list, list) {
  378. if (hwirq-- == 0)
  379. break;
  380. irq_set_msi_desc(msi->irq, NULL);
  381. irq_free_desc(msi->irq);
  382. msi->msg.address_lo = 0;
  383. msi->msg.address_hi = 0;
  384. msi->msg.data = 0;
  385. msi->irq = 0;
  386. }
  387. zpci_aibv[aisb] = NULL;
  388. airq_iv_release(zdev->aibv);
  389. out_si:
  390. airq_iv_free_bit(zpci_aisb_iv, aisb);
  391. out:
  392. return rc;
  393. }
  394. void arch_teardown_msi_irqs(struct pci_dev *pdev)
  395. {
  396. struct zpci_dev *zdev = get_zdev(pdev);
  397. struct msi_desc *msi;
  398. int rc;
  399. /* Disable adapter interrupts */
  400. rc = zpci_clear_airq(zdev);
  401. if (rc)
  402. return;
  403. /* Release MSI interrupts */
  404. list_for_each_entry(msi, &pdev->msi_list, list) {
  405. zpci_msi_set_mask_bits(msi, 1, 1);
  406. irq_set_msi_desc(msi->irq, NULL);
  407. irq_free_desc(msi->irq);
  408. msi->msg.address_lo = 0;
  409. msi->msg.address_hi = 0;
  410. msi->msg.data = 0;
  411. msi->irq = 0;
  412. }
  413. zpci_aibv[zdev->aisb] = NULL;
  414. airq_iv_release(zdev->aibv);
  415. airq_iv_free_bit(zpci_aisb_iv, zdev->aisb);
  416. }
  417. static void zpci_map_resources(struct zpci_dev *zdev)
  418. {
  419. struct pci_dev *pdev = zdev->pdev;
  420. resource_size_t len;
  421. int i;
  422. for (i = 0; i < PCI_BAR_COUNT; i++) {
  423. len = pci_resource_len(pdev, i);
  424. if (!len)
  425. continue;
  426. pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
  427. pdev->resource[i].end = pdev->resource[i].start + len - 1;
  428. }
  429. }
  430. static void zpci_unmap_resources(struct zpci_dev *zdev)
  431. {
  432. struct pci_dev *pdev = zdev->pdev;
  433. resource_size_t len;
  434. int i;
  435. for (i = 0; i < PCI_BAR_COUNT; i++) {
  436. len = pci_resource_len(pdev, i);
  437. if (!len)
  438. continue;
  439. pci_iounmap(pdev, (void *) pdev->resource[i].start);
  440. }
  441. }
  442. int pcibios_add_platform_entries(struct pci_dev *pdev)
  443. {
  444. return zpci_sysfs_add_device(&pdev->dev);
  445. }
  446. static int __init zpci_irq_init(void)
  447. {
  448. int rc;
  449. rc = register_adapter_interrupt(&zpci_airq);
  450. if (rc)
  451. goto out;
  452. /* Set summary to 1 to be called every time for the ISC. */
  453. *zpci_airq.lsi_ptr = 1;
  454. rc = -ENOMEM;
  455. zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC);
  456. if (!zpci_aisb_iv)
  457. goto out_airq;
  458. zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  459. return 0;
  460. out_airq:
  461. unregister_adapter_interrupt(&zpci_airq);
  462. out:
  463. return rc;
  464. }
  465. static void zpci_irq_exit(void)
  466. {
  467. airq_iv_release(zpci_aisb_iv);
  468. unregister_adapter_interrupt(&zpci_airq);
  469. }
  470. static int zpci_alloc_iomap(struct zpci_dev *zdev)
  471. {
  472. int entry;
  473. spin_lock(&zpci_iomap_lock);
  474. entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  475. if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
  476. spin_unlock(&zpci_iomap_lock);
  477. return -ENOSPC;
  478. }
  479. set_bit(entry, zpci_iomap);
  480. spin_unlock(&zpci_iomap_lock);
  481. return entry;
  482. }
  483. static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
  484. {
  485. spin_lock(&zpci_iomap_lock);
  486. memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
  487. clear_bit(entry, zpci_iomap);
  488. spin_unlock(&zpci_iomap_lock);
  489. }
  490. static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
  491. unsigned long size, unsigned long flags)
  492. {
  493. struct resource *r;
  494. r = kzalloc(sizeof(*r), GFP_KERNEL);
  495. if (!r)
  496. return NULL;
  497. r->start = start;
  498. r->end = r->start + size - 1;
  499. r->flags = flags;
  500. r->name = zdev->res_name;
  501. if (request_resource(&iomem_resource, r)) {
  502. kfree(r);
  503. return NULL;
  504. }
  505. return r;
  506. }
  507. static int zpci_setup_bus_resources(struct zpci_dev *zdev,
  508. struct list_head *resources)
  509. {
  510. unsigned long addr, size, flags;
  511. struct resource *res;
  512. int i, entry;
  513. snprintf(zdev->res_name, sizeof(zdev->res_name),
  514. "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);
  515. for (i = 0; i < PCI_BAR_COUNT; i++) {
  516. if (!zdev->bars[i].size)
  517. continue;
  518. entry = zpci_alloc_iomap(zdev);
  519. if (entry < 0)
  520. return entry;
  521. zdev->bars[i].map_idx = entry;
  522. /* only MMIO is supported */
  523. flags = IORESOURCE_MEM;
  524. if (zdev->bars[i].val & 8)
  525. flags |= IORESOURCE_PREFETCH;
  526. if (zdev->bars[i].val & 4)
  527. flags |= IORESOURCE_MEM_64;
  528. addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
  529. size = 1UL << zdev->bars[i].size;
  530. res = __alloc_res(zdev, addr, size, flags);
  531. if (!res) {
  532. zpci_free_iomap(zdev, entry);
  533. return -ENOMEM;
  534. }
  535. zdev->bars[i].res = res;
  536. pci_add_resource(resources, res);
  537. }
  538. return 0;
  539. }
  540. static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
  541. {
  542. int i;
  543. for (i = 0; i < PCI_BAR_COUNT; i++) {
  544. if (!zdev->bars[i].size)
  545. continue;
  546. zpci_free_iomap(zdev, zdev->bars[i].map_idx);
  547. release_resource(zdev->bars[i].res);
  548. kfree(zdev->bars[i].res);
  549. }
  550. }
  551. int pcibios_add_device(struct pci_dev *pdev)
  552. {
  553. struct zpci_dev *zdev = get_zdev(pdev);
  554. struct resource *res;
  555. int i;
  556. zdev->pdev = pdev;
  557. zpci_map_resources(zdev);
  558. for (i = 0; i < PCI_BAR_COUNT; i++) {
  559. res = &pdev->resource[i];
  560. if (res->parent || !res->flags)
  561. continue;
  562. pci_claim_resource(pdev, i);
  563. }
  564. return 0;
  565. }
  566. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  567. {
  568. struct zpci_dev *zdev = get_zdev(pdev);
  569. struct resource *res;
  570. u16 cmd;
  571. int i;
  572. zdev->pdev = pdev;
  573. zpci_debug_init_device(zdev);
  574. zpci_fmb_enable_device(zdev);
  575. zpci_map_resources(zdev);
  576. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  577. for (i = 0; i < PCI_BAR_COUNT; i++) {
  578. res = &pdev->resource[i];
  579. if (res->flags & IORESOURCE_IO)
  580. return -EINVAL;
  581. if (res->flags & IORESOURCE_MEM)
  582. cmd |= PCI_COMMAND_MEMORY;
  583. }
  584. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  585. return 0;
  586. }
  587. void pcibios_disable_device(struct pci_dev *pdev)
  588. {
  589. struct zpci_dev *zdev = get_zdev(pdev);
  590. zpci_unmap_resources(zdev);
  591. zpci_fmb_disable_device(zdev);
  592. zpci_debug_exit_device(zdev);
  593. zdev->pdev = NULL;
  594. }
  595. #ifdef CONFIG_HIBERNATE_CALLBACKS
  596. static int zpci_restore(struct device *dev)
  597. {
  598. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  599. int ret = 0;
  600. if (zdev->state != ZPCI_FN_STATE_ONLINE)
  601. goto out;
  602. ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  603. if (ret)
  604. goto out;
  605. zpci_map_resources(zdev);
  606. zpci_register_ioat(zdev, 0, zdev->start_dma + PAGE_OFFSET,
  607. zdev->start_dma + zdev->iommu_size - 1,
  608. (u64) zdev->dma_table);
  609. out:
  610. return ret;
  611. }
  612. static int zpci_freeze(struct device *dev)
  613. {
  614. struct zpci_dev *zdev = get_zdev(to_pci_dev(dev));
  615. if (zdev->state != ZPCI_FN_STATE_ONLINE)
  616. return 0;
  617. zpci_unregister_ioat(zdev, 0);
  618. return clp_disable_fh(zdev);
  619. }
  620. struct dev_pm_ops pcibios_pm_ops = {
  621. .thaw_noirq = zpci_restore,
  622. .freeze_noirq = zpci_freeze,
  623. .restore_noirq = zpci_restore,
  624. .poweroff_noirq = zpci_freeze,
  625. };
  626. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  627. static int zpci_alloc_domain(struct zpci_dev *zdev)
  628. {
  629. spin_lock(&zpci_domain_lock);
  630. zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
  631. if (zdev->domain == ZPCI_NR_DEVICES) {
  632. spin_unlock(&zpci_domain_lock);
  633. return -ENOSPC;
  634. }
  635. set_bit(zdev->domain, zpci_domain);
  636. spin_unlock(&zpci_domain_lock);
  637. return 0;
  638. }
  639. static void zpci_free_domain(struct zpci_dev *zdev)
  640. {
  641. spin_lock(&zpci_domain_lock);
  642. clear_bit(zdev->domain, zpci_domain);
  643. spin_unlock(&zpci_domain_lock);
  644. }
  645. void pcibios_remove_bus(struct pci_bus *bus)
  646. {
  647. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  648. zpci_exit_slot(zdev);
  649. zpci_cleanup_bus_resources(zdev);
  650. zpci_free_domain(zdev);
  651. spin_lock(&zpci_list_lock);
  652. list_del(&zdev->entry);
  653. spin_unlock(&zpci_list_lock);
  654. kfree(zdev);
  655. }
  656. static int zpci_scan_bus(struct zpci_dev *zdev)
  657. {
  658. LIST_HEAD(resources);
  659. int ret;
  660. ret = zpci_setup_bus_resources(zdev, &resources);
  661. if (ret)
  662. return ret;
  663. zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
  664. zdev, &resources);
  665. if (!zdev->bus) {
  666. zpci_cleanup_bus_resources(zdev);
  667. return -EIO;
  668. }
  669. zdev->bus->max_bus_speed = zdev->max_bus_speed;
  670. return 0;
  671. }
  672. int zpci_enable_device(struct zpci_dev *zdev)
  673. {
  674. int rc;
  675. rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  676. if (rc)
  677. goto out;
  678. rc = zpci_dma_init_device(zdev);
  679. if (rc)
  680. goto out_dma;
  681. zdev->state = ZPCI_FN_STATE_ONLINE;
  682. return 0;
  683. out_dma:
  684. clp_disable_fh(zdev);
  685. out:
  686. return rc;
  687. }
  688. EXPORT_SYMBOL_GPL(zpci_enable_device);
  689. int zpci_disable_device(struct zpci_dev *zdev)
  690. {
  691. zpci_dma_exit_device(zdev);
  692. return clp_disable_fh(zdev);
  693. }
  694. EXPORT_SYMBOL_GPL(zpci_disable_device);
  695. int zpci_create_device(struct zpci_dev *zdev)
  696. {
  697. int rc;
  698. rc = zpci_alloc_domain(zdev);
  699. if (rc)
  700. goto out;
  701. if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
  702. rc = zpci_enable_device(zdev);
  703. if (rc)
  704. goto out_free;
  705. }
  706. rc = zpci_scan_bus(zdev);
  707. if (rc)
  708. goto out_disable;
  709. spin_lock(&zpci_list_lock);
  710. list_add_tail(&zdev->entry, &zpci_list);
  711. spin_unlock(&zpci_list_lock);
  712. zpci_init_slot(zdev);
  713. return 0;
  714. out_disable:
  715. if (zdev->state == ZPCI_FN_STATE_ONLINE)
  716. zpci_disable_device(zdev);
  717. out_free:
  718. zpci_free_domain(zdev);
  719. out:
  720. return rc;
  721. }
  722. void zpci_stop_device(struct zpci_dev *zdev)
  723. {
  724. zpci_dma_exit_device(zdev);
  725. /*
  726. * Note: SCLP disables fh via set-pci-fn so don't
  727. * do that here.
  728. */
  729. }
  730. EXPORT_SYMBOL_GPL(zpci_stop_device);
  731. static inline int barsize(u8 size)
  732. {
  733. return (size) ? (1 << size) >> 10 : 0;
  734. }
  735. static int zpci_mem_init(void)
  736. {
  737. zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
  738. 16, 0, NULL);
  739. if (!zdev_fmb_cache)
  740. goto error_zdev;
  741. /* TODO: use realloc */
  742. zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
  743. GFP_KERNEL);
  744. if (!zpci_iomap_start)
  745. goto error_iomap;
  746. return 0;
  747. error_iomap:
  748. kmem_cache_destroy(zdev_fmb_cache);
  749. error_zdev:
  750. return -ENOMEM;
  751. }
  752. static void zpci_mem_exit(void)
  753. {
  754. kfree(zpci_iomap_start);
  755. kmem_cache_destroy(zdev_fmb_cache);
  756. }
  757. static unsigned int s390_pci_probe;
  758. char * __init pcibios_setup(char *str)
  759. {
  760. if (!strcmp(str, "on")) {
  761. s390_pci_probe = 1;
  762. return NULL;
  763. }
  764. return str;
  765. }
  766. static int __init pci_base_init(void)
  767. {
  768. int rc;
  769. if (!s390_pci_probe)
  770. return 0;
  771. if (!test_facility(2) || !test_facility(69)
  772. || !test_facility(71) || !test_facility(72))
  773. return 0;
  774. rc = zpci_debug_init();
  775. if (rc)
  776. goto out;
  777. rc = zpci_mem_init();
  778. if (rc)
  779. goto out_mem;
  780. rc = zpci_irq_init();
  781. if (rc)
  782. goto out_irq;
  783. rc = zpci_dma_init();
  784. if (rc)
  785. goto out_dma;
  786. rc = clp_scan_pci_devices();
  787. if (rc)
  788. goto out_find;
  789. return 0;
  790. out_find:
  791. zpci_dma_exit();
  792. out_dma:
  793. zpci_irq_exit();
  794. out_irq:
  795. zpci_mem_exit();
  796. out_mem:
  797. zpci_debug_exit();
  798. out:
  799. return rc;
  800. }
  801. subsys_initcall_sync(pci_base_init);
  802. void zpci_rescan(void)
  803. {
  804. clp_rescan_pci_devices_simple();
  805. }