pci.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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_MSI_VEC_BITS 6
  40. #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
  41. /* list of all detected zpci devices */
  42. LIST_HEAD(zpci_list);
  43. EXPORT_SYMBOL_GPL(zpci_list);
  44. DEFINE_MUTEX(zpci_list_lock);
  45. EXPORT_SYMBOL_GPL(zpci_list_lock);
  46. static struct pci_hp_callback_ops *hotplug_ops;
  47. static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
  48. static DEFINE_SPINLOCK(zpci_domain_lock);
  49. struct callback {
  50. irq_handler_t handler;
  51. void *data;
  52. };
  53. struct zdev_irq_map {
  54. unsigned long aibv; /* AI bit vector */
  55. int msi_vecs; /* consecutive MSI-vectors used */
  56. int __unused;
  57. struct callback cb[ZPCI_NR_MSI_VECS]; /* callback handler array */
  58. spinlock_t lock; /* protect callbacks against de-reg */
  59. };
  60. struct intr_bucket {
  61. /* amap of adapters, one bit per dev, corresponds to one irq nr */
  62. unsigned long *alloc;
  63. /* AI summary bit, global page for all devices */
  64. unsigned long *aisb;
  65. /* pointer to aibv and callback data in zdev */
  66. struct zdev_irq_map *imap[ZPCI_NR_DEVICES];
  67. /* protects the whole bucket struct */
  68. spinlock_t lock;
  69. };
  70. static struct intr_bucket *bucket;
  71. /* Adapter interrupt definitions */
  72. static void zpci_irq_handler(struct airq_struct *airq);
  73. static struct airq_struct zpci_airq = {
  74. .handler = zpci_irq_handler,
  75. .isc = PCI_ISC,
  76. };
  77. /* I/O Map */
  78. static DEFINE_SPINLOCK(zpci_iomap_lock);
  79. static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  80. struct zpci_iomap_entry *zpci_iomap_start;
  81. EXPORT_SYMBOL_GPL(zpci_iomap_start);
  82. /* highest irq summary bit */
  83. static int __read_mostly aisb_max;
  84. static struct kmem_cache *zdev_irq_cache;
  85. static struct kmem_cache *zdev_fmb_cache;
  86. static inline int irq_to_msi_nr(unsigned int irq)
  87. {
  88. return irq & ZPCI_MSI_MASK;
  89. }
  90. static inline int irq_to_dev_nr(unsigned int irq)
  91. {
  92. return irq >> ZPCI_MSI_VEC_BITS;
  93. }
  94. static inline struct zdev_irq_map *get_imap(unsigned int irq)
  95. {
  96. return bucket->imap[irq_to_dev_nr(irq)];
  97. }
  98. struct zpci_dev *get_zdev(struct pci_dev *pdev)
  99. {
  100. return (struct zpci_dev *) pdev->sysdata;
  101. }
  102. struct zpci_dev *get_zdev_by_fid(u32 fid)
  103. {
  104. struct zpci_dev *tmp, *zdev = NULL;
  105. mutex_lock(&zpci_list_lock);
  106. list_for_each_entry(tmp, &zpci_list, entry) {
  107. if (tmp->fid == fid) {
  108. zdev = tmp;
  109. break;
  110. }
  111. }
  112. mutex_unlock(&zpci_list_lock);
  113. return zdev;
  114. }
  115. bool zpci_fid_present(u32 fid)
  116. {
  117. return (get_zdev_by_fid(fid) != NULL) ? true : false;
  118. }
  119. static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
  120. {
  121. return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
  122. }
  123. int pci_domain_nr(struct pci_bus *bus)
  124. {
  125. return ((struct zpci_dev *) bus->sysdata)->domain;
  126. }
  127. EXPORT_SYMBOL_GPL(pci_domain_nr);
  128. int pci_proc_domain(struct pci_bus *bus)
  129. {
  130. return pci_domain_nr(bus);
  131. }
  132. EXPORT_SYMBOL_GPL(pci_proc_domain);
  133. /* Modify PCI: Register adapter interruptions */
  134. static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb,
  135. u64 aibv)
  136. {
  137. u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
  138. struct zpci_fib *fib;
  139. int rc;
  140. fib = (void *) get_zeroed_page(GFP_KERNEL);
  141. if (!fib)
  142. return -ENOMEM;
  143. fib->isc = PCI_ISC;
  144. fib->noi = zdev->irq_map->msi_vecs;
  145. fib->sum = 1; /* enable summary notifications */
  146. fib->aibv = aibv;
  147. fib->aibvo = 0; /* every function has its own page */
  148. fib->aisb = (u64) bucket->aisb + aisb / 8;
  149. fib->aisbo = aisb & ZPCI_MSI_MASK;
  150. rc = s390pci_mod_fc(req, fib);
  151. pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi);
  152. free_page((unsigned long) fib);
  153. return rc;
  154. }
  155. struct mod_pci_args {
  156. u64 base;
  157. u64 limit;
  158. u64 iota;
  159. u64 fmb_addr;
  160. };
  161. static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
  162. {
  163. u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
  164. struct zpci_fib *fib;
  165. int rc;
  166. /* The FIB must be available even if it's not used */
  167. fib = (void *) get_zeroed_page(GFP_KERNEL);
  168. if (!fib)
  169. return -ENOMEM;
  170. fib->pba = args->base;
  171. fib->pal = args->limit;
  172. fib->iota = args->iota;
  173. fib->fmb_addr = args->fmb_addr;
  174. rc = s390pci_mod_fc(req, fib);
  175. free_page((unsigned long) fib);
  176. return rc;
  177. }
  178. /* Modify PCI: Register I/O address translation parameters */
  179. int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
  180. u64 base, u64 limit, u64 iota)
  181. {
  182. struct mod_pci_args args = { base, limit, iota, 0 };
  183. WARN_ON_ONCE(iota & 0x3fff);
  184. args.iota |= ZPCI_IOTA_RTTO_FLAG;
  185. return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
  186. }
  187. /* Modify PCI: Unregister I/O address translation parameters */
  188. int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
  189. {
  190. struct mod_pci_args args = { 0, 0, 0, 0 };
  191. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
  192. }
  193. /* Modify PCI: Unregister adapter interruptions */
  194. static int zpci_unregister_airq(struct zpci_dev *zdev)
  195. {
  196. struct mod_pci_args args = { 0, 0, 0, 0 };
  197. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
  198. }
  199. /* Modify PCI: Set PCI function measurement parameters */
  200. int zpci_fmb_enable_device(struct zpci_dev *zdev)
  201. {
  202. struct mod_pci_args args = { 0, 0, 0, 0 };
  203. if (zdev->fmb)
  204. return -EINVAL;
  205. zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
  206. if (!zdev->fmb)
  207. return -ENOMEM;
  208. WARN_ON((u64) zdev->fmb & 0xf);
  209. args.fmb_addr = virt_to_phys(zdev->fmb);
  210. return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  211. }
  212. /* Modify PCI: Disable PCI function measurement */
  213. int zpci_fmb_disable_device(struct zpci_dev *zdev)
  214. {
  215. struct mod_pci_args args = { 0, 0, 0, 0 };
  216. int rc;
  217. if (!zdev->fmb)
  218. return -EINVAL;
  219. /* Function measurement is disabled if fmb address is zero */
  220. rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  221. kmem_cache_free(zdev_fmb_cache, zdev->fmb);
  222. zdev->fmb = NULL;
  223. return rc;
  224. }
  225. #define ZPCI_PCIAS_CFGSPC 15
  226. static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
  227. {
  228. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  229. u64 data;
  230. int rc;
  231. rc = s390pci_load(&data, req, offset);
  232. if (!rc) {
  233. data = data << ((8 - len) * 8);
  234. data = le64_to_cpu(data);
  235. *val = (u32) data;
  236. } else
  237. *val = 0xffffffff;
  238. return rc;
  239. }
  240. static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
  241. {
  242. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  243. u64 data = val;
  244. int rc;
  245. data = cpu_to_le64(data);
  246. data = data >> ((8 - len) * 8);
  247. rc = s390pci_store(data, req, offset);
  248. return rc;
  249. }
  250. void enable_irq(unsigned int irq)
  251. {
  252. struct msi_desc *msi = irq_get_msi_desc(irq);
  253. zpci_msi_set_mask_bits(msi, 1, 0);
  254. }
  255. EXPORT_SYMBOL_GPL(enable_irq);
  256. void disable_irq(unsigned int irq)
  257. {
  258. struct msi_desc *msi = irq_get_msi_desc(irq);
  259. zpci_msi_set_mask_bits(msi, 1, 1);
  260. }
  261. EXPORT_SYMBOL_GPL(disable_irq);
  262. void pcibios_fixup_bus(struct pci_bus *bus)
  263. {
  264. }
  265. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  266. resource_size_t size,
  267. resource_size_t align)
  268. {
  269. return 0;
  270. }
  271. /* combine single writes by using store-block insn */
  272. void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
  273. {
  274. zpci_memcpy_toio(to, from, count);
  275. }
  276. /* Create a virtual mapping cookie for a PCI BAR */
  277. void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
  278. {
  279. struct zpci_dev *zdev = get_zdev(pdev);
  280. u64 addr;
  281. int idx;
  282. if ((bar & 7) != bar)
  283. return NULL;
  284. idx = zdev->bars[bar].map_idx;
  285. spin_lock(&zpci_iomap_lock);
  286. zpci_iomap_start[idx].fh = zdev->fh;
  287. zpci_iomap_start[idx].bar = bar;
  288. spin_unlock(&zpci_iomap_lock);
  289. addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
  290. return (void __iomem *) addr;
  291. }
  292. EXPORT_SYMBOL_GPL(pci_iomap);
  293. void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
  294. {
  295. unsigned int idx;
  296. idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
  297. spin_lock(&zpci_iomap_lock);
  298. zpci_iomap_start[idx].fh = 0;
  299. zpci_iomap_start[idx].bar = 0;
  300. spin_unlock(&zpci_iomap_lock);
  301. }
  302. EXPORT_SYMBOL_GPL(pci_iounmap);
  303. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  304. int size, u32 *val)
  305. {
  306. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  307. int ret;
  308. if (!zdev || devfn != ZPCI_DEVFN)
  309. ret = -ENODEV;
  310. else
  311. ret = zpci_cfg_load(zdev, where, val, size);
  312. return ret;
  313. }
  314. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  315. int size, u32 val)
  316. {
  317. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  318. int ret;
  319. if (!zdev || devfn != ZPCI_DEVFN)
  320. ret = -ENODEV;
  321. else
  322. ret = zpci_cfg_store(zdev, where, val, size);
  323. return ret;
  324. }
  325. static struct pci_ops pci_root_ops = {
  326. .read = pci_read,
  327. .write = pci_write,
  328. };
  329. /* store the last handled bit to implement fair scheduling of devices */
  330. static DEFINE_PER_CPU(unsigned long, next_sbit);
  331. static void zpci_irq_handler(struct airq_struct *airq)
  332. {
  333. unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit);
  334. int rescan = 0, max = aisb_max;
  335. struct zdev_irq_map *imap;
  336. inc_irq_stat(IRQIO_PCI);
  337. sbit = start;
  338. scan:
  339. /* find summary_bit */
  340. for_each_set_bit_left_cont(sbit, bucket->aisb, max) {
  341. clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6));
  342. last = sbit;
  343. /* find vector bit */
  344. imap = bucket->imap[sbit];
  345. for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) {
  346. inc_irq_stat(IRQIO_MSI);
  347. clear_bit(63 - mbit, &imap->aibv);
  348. spin_lock(&imap->lock);
  349. if (imap->cb[mbit].handler)
  350. imap->cb[mbit].handler(mbit,
  351. imap->cb[mbit].data);
  352. spin_unlock(&imap->lock);
  353. }
  354. }
  355. if (rescan)
  356. goto out;
  357. /* scan the skipped bits */
  358. if (start > 0) {
  359. sbit = 0;
  360. max = start;
  361. start = 0;
  362. goto scan;
  363. }
  364. /* enable interrupts again */
  365. set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  366. /* check again to not lose initiative */
  367. rmb();
  368. max = aisb_max;
  369. sbit = find_first_bit_left(bucket->aisb, max);
  370. if (sbit != max) {
  371. rescan++;
  372. goto scan;
  373. }
  374. out:
  375. /* store next device bit to scan */
  376. __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last;
  377. }
  378. /* msi_vecs - number of requested interrupts, 0 place function to error state */
  379. static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs)
  380. {
  381. struct zpci_dev *zdev = get_zdev(pdev);
  382. unsigned int aisb, msi_nr;
  383. struct msi_desc *msi;
  384. int rc;
  385. /* store the number of used MSI vectors */
  386. zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS);
  387. spin_lock(&bucket->lock);
  388. aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE);
  389. /* alloc map exhausted? */
  390. if (aisb == PAGE_SIZE) {
  391. spin_unlock(&bucket->lock);
  392. return -EIO;
  393. }
  394. set_bit(aisb, bucket->alloc);
  395. spin_unlock(&bucket->lock);
  396. zdev->aisb = aisb;
  397. if (aisb + 1 > aisb_max)
  398. aisb_max = aisb + 1;
  399. /* wire up IRQ shortcut pointer */
  400. bucket->imap[zdev->aisb] = zdev->irq_map;
  401. pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map);
  402. /* TODO: irq number 0 wont be found if we return less than requested MSIs.
  403. * ignore it for now and fix in common code.
  404. */
  405. msi_nr = aisb << ZPCI_MSI_VEC_BITS;
  406. list_for_each_entry(msi, &pdev->msi_list, list) {
  407. rc = zpci_setup_msi_irq(zdev, msi, msi_nr,
  408. aisb << ZPCI_MSI_VEC_BITS);
  409. if (rc)
  410. return rc;
  411. msi_nr++;
  412. }
  413. rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv);
  414. if (rc) {
  415. clear_bit(aisb, bucket->alloc);
  416. dev_err(&pdev->dev, "register MSI failed with: %d\n", rc);
  417. return rc;
  418. }
  419. return (zdev->irq_map->msi_vecs == msi_vecs) ?
  420. 0 : zdev->irq_map->msi_vecs;
  421. }
  422. static void zpci_teardown_msi(struct pci_dev *pdev)
  423. {
  424. struct zpci_dev *zdev = get_zdev(pdev);
  425. struct msi_desc *msi;
  426. int aisb, rc;
  427. rc = zpci_unregister_airq(zdev);
  428. if (rc) {
  429. dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc);
  430. return;
  431. }
  432. msi = list_first_entry(&pdev->msi_list, struct msi_desc, list);
  433. aisb = irq_to_dev_nr(msi->irq);
  434. list_for_each_entry(msi, &pdev->msi_list, list)
  435. zpci_teardown_msi_irq(zdev, msi);
  436. clear_bit(aisb, bucket->alloc);
  437. if (aisb + 1 == aisb_max)
  438. aisb_max--;
  439. }
  440. int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  441. {
  442. pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec);
  443. if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI)
  444. return -EINVAL;
  445. return zpci_setup_msi(pdev, nvec);
  446. }
  447. void arch_teardown_msi_irqs(struct pci_dev *pdev)
  448. {
  449. pr_info("%s: on pdev: %p\n", __func__, pdev);
  450. zpci_teardown_msi(pdev);
  451. }
  452. static void zpci_map_resources(struct zpci_dev *zdev)
  453. {
  454. struct pci_dev *pdev = zdev->pdev;
  455. resource_size_t len;
  456. int i;
  457. for (i = 0; i < PCI_BAR_COUNT; i++) {
  458. len = pci_resource_len(pdev, i);
  459. if (!len)
  460. continue;
  461. pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
  462. pdev->resource[i].end = pdev->resource[i].start + len - 1;
  463. pr_debug("BAR%i: -> start: %Lx end: %Lx\n",
  464. i, pdev->resource[i].start, pdev->resource[i].end);
  465. }
  466. }
  467. static void zpci_unmap_resources(struct zpci_dev *zdev)
  468. {
  469. struct pci_dev *pdev = zdev->pdev;
  470. resource_size_t len;
  471. int i;
  472. for (i = 0; i < PCI_BAR_COUNT; i++) {
  473. len = pci_resource_len(pdev, i);
  474. if (!len)
  475. continue;
  476. pci_iounmap(pdev, (void *) pdev->resource[i].start);
  477. }
  478. }
  479. struct zpci_dev *zpci_alloc_device(void)
  480. {
  481. struct zpci_dev *zdev;
  482. /* Alloc memory for our private pci device data */
  483. zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
  484. if (!zdev)
  485. return ERR_PTR(-ENOMEM);
  486. /* Alloc aibv & callback space */
  487. zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL);
  488. if (!zdev->irq_map)
  489. goto error;
  490. WARN_ON((u64) zdev->irq_map & 0xff);
  491. return zdev;
  492. error:
  493. kfree(zdev);
  494. return ERR_PTR(-ENOMEM);
  495. }
  496. void zpci_free_device(struct zpci_dev *zdev)
  497. {
  498. kmem_cache_free(zdev_irq_cache, zdev->irq_map);
  499. kfree(zdev);
  500. }
  501. /*
  502. * Too late for any s390 specific setup, since interrupts must be set up
  503. * already which requires DMA setup too and the pci scan will access the
  504. * config space, which only works if the function handle is enabled.
  505. */
  506. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  507. {
  508. struct resource *res;
  509. u16 cmd;
  510. int i;
  511. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  512. for (i = 0; i < PCI_BAR_COUNT; i++) {
  513. res = &pdev->resource[i];
  514. if (res->flags & IORESOURCE_IO)
  515. return -EINVAL;
  516. if (res->flags & IORESOURCE_MEM)
  517. cmd |= PCI_COMMAND_MEMORY;
  518. }
  519. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  520. return 0;
  521. }
  522. int pcibios_add_platform_entries(struct pci_dev *pdev)
  523. {
  524. return zpci_sysfs_add_device(&pdev->dev);
  525. }
  526. int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data)
  527. {
  528. int msi_nr = irq_to_msi_nr(irq);
  529. struct zdev_irq_map *imap;
  530. struct msi_desc *msi;
  531. msi = irq_get_msi_desc(irq);
  532. if (!msi)
  533. return -EIO;
  534. imap = get_imap(irq);
  535. spin_lock_init(&imap->lock);
  536. pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr);
  537. imap->cb[msi_nr].handler = handler;
  538. imap->cb[msi_nr].data = data;
  539. /*
  540. * The generic MSI code returns with the interrupt disabled on the
  541. * card, using the MSI mask bits. Firmware doesn't appear to unmask
  542. * at that level, so we do it here by hand.
  543. */
  544. zpci_msi_set_mask_bits(msi, 1, 0);
  545. return 0;
  546. }
  547. void zpci_free_irq(unsigned int irq)
  548. {
  549. struct zdev_irq_map *imap = get_imap(irq);
  550. int msi_nr = irq_to_msi_nr(irq);
  551. unsigned long flags;
  552. pr_debug("%s: for irq: %d\n", __func__, irq);
  553. spin_lock_irqsave(&imap->lock, flags);
  554. imap->cb[msi_nr].handler = NULL;
  555. imap->cb[msi_nr].data = NULL;
  556. spin_unlock_irqrestore(&imap->lock, flags);
  557. }
  558. int request_irq(unsigned int irq, irq_handler_t handler,
  559. unsigned long irqflags, const char *devname, void *dev_id)
  560. {
  561. pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n",
  562. __func__, irq, handler, irqflags, devname);
  563. return zpci_request_irq(irq, handler, dev_id);
  564. }
  565. EXPORT_SYMBOL_GPL(request_irq);
  566. void free_irq(unsigned int irq, void *dev_id)
  567. {
  568. zpci_free_irq(irq);
  569. }
  570. EXPORT_SYMBOL_GPL(free_irq);
  571. static int __init zpci_irq_init(void)
  572. {
  573. int cpu, rc;
  574. bucket = kzalloc(sizeof(*bucket), GFP_KERNEL);
  575. if (!bucket)
  576. return -ENOMEM;
  577. bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  578. if (!bucket->aisb) {
  579. rc = -ENOMEM;
  580. goto out_aisb;
  581. }
  582. bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  583. if (!bucket->alloc) {
  584. rc = -ENOMEM;
  585. goto out_alloc;
  586. }
  587. rc = register_adapter_interrupt(&zpci_airq);
  588. if (rc)
  589. goto out_ai;
  590. /* Set summary to 1 to be called every time for the ISC. */
  591. *zpci_airq.lsi_ptr = 1;
  592. for_each_online_cpu(cpu)
  593. per_cpu(next_sbit, cpu) = 0;
  594. spin_lock_init(&bucket->lock);
  595. set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  596. return 0;
  597. out_ai:
  598. free_page((unsigned long) bucket->alloc);
  599. out_alloc:
  600. free_page((unsigned long) bucket->aisb);
  601. out_aisb:
  602. kfree(bucket);
  603. return rc;
  604. }
  605. static void zpci_irq_exit(void)
  606. {
  607. free_page((unsigned long) bucket->alloc);
  608. free_page((unsigned long) bucket->aisb);
  609. unregister_adapter_interrupt(&zpci_airq);
  610. kfree(bucket);
  611. }
  612. static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
  613. unsigned long flags, int domain)
  614. {
  615. struct resource *r;
  616. char *name;
  617. int rc;
  618. r = kzalloc(sizeof(*r), GFP_KERNEL);
  619. if (!r)
  620. return ERR_PTR(-ENOMEM);
  621. r->start = start;
  622. r->end = r->start + size - 1;
  623. r->flags = flags;
  624. r->parent = &iomem_resource;
  625. name = kmalloc(18, GFP_KERNEL);
  626. if (!name) {
  627. kfree(r);
  628. return ERR_PTR(-ENOMEM);
  629. }
  630. sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
  631. r->name = name;
  632. rc = request_resource(&iomem_resource, r);
  633. if (rc)
  634. pr_debug("request resource %pR failed\n", r);
  635. return r;
  636. }
  637. static int zpci_alloc_iomap(struct zpci_dev *zdev)
  638. {
  639. int entry;
  640. spin_lock(&zpci_iomap_lock);
  641. entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  642. if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
  643. spin_unlock(&zpci_iomap_lock);
  644. return -ENOSPC;
  645. }
  646. set_bit(entry, zpci_iomap);
  647. spin_unlock(&zpci_iomap_lock);
  648. return entry;
  649. }
  650. static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
  651. {
  652. spin_lock(&zpci_iomap_lock);
  653. memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
  654. clear_bit(entry, zpci_iomap);
  655. spin_unlock(&zpci_iomap_lock);
  656. }
  657. int pcibios_add_device(struct pci_dev *pdev)
  658. {
  659. struct zpci_dev *zdev = get_zdev(pdev);
  660. zdev->pdev = pdev;
  661. zpci_debug_init_device(zdev);
  662. zpci_fmb_enable_device(zdev);
  663. zpci_map_resources(zdev);
  664. return 0;
  665. }
  666. void pcibios_release_device(struct pci_dev *pdev)
  667. {
  668. struct zpci_dev *zdev = get_zdev(pdev);
  669. zpci_unmap_resources(zdev);
  670. zpci_fmb_disable_device(zdev);
  671. zpci_debug_exit_device(zdev);
  672. zdev->pdev = NULL;
  673. }
  674. static int zpci_scan_bus(struct zpci_dev *zdev)
  675. {
  676. struct resource *res;
  677. LIST_HEAD(resources);
  678. int i;
  679. /* allocate mapping entry for each used bar */
  680. for (i = 0; i < PCI_BAR_COUNT; i++) {
  681. unsigned long addr, size, flags;
  682. int entry;
  683. if (!zdev->bars[i].size)
  684. continue;
  685. entry = zpci_alloc_iomap(zdev);
  686. if (entry < 0)
  687. return entry;
  688. zdev->bars[i].map_idx = entry;
  689. /* only MMIO is supported */
  690. flags = IORESOURCE_MEM;
  691. if (zdev->bars[i].val & 8)
  692. flags |= IORESOURCE_PREFETCH;
  693. if (zdev->bars[i].val & 4)
  694. flags |= IORESOURCE_MEM_64;
  695. addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
  696. size = 1UL << zdev->bars[i].size;
  697. res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
  698. if (IS_ERR(res)) {
  699. zpci_free_iomap(zdev, entry);
  700. return PTR_ERR(res);
  701. }
  702. pci_add_resource(&resources, res);
  703. }
  704. zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
  705. zdev, &resources);
  706. if (!zdev->bus)
  707. return -EIO;
  708. zdev->bus->max_bus_speed = zdev->max_bus_speed;
  709. return 0;
  710. }
  711. static int zpci_alloc_domain(struct zpci_dev *zdev)
  712. {
  713. spin_lock(&zpci_domain_lock);
  714. zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
  715. if (zdev->domain == ZPCI_NR_DEVICES) {
  716. spin_unlock(&zpci_domain_lock);
  717. return -ENOSPC;
  718. }
  719. set_bit(zdev->domain, zpci_domain);
  720. spin_unlock(&zpci_domain_lock);
  721. return 0;
  722. }
  723. static void zpci_free_domain(struct zpci_dev *zdev)
  724. {
  725. spin_lock(&zpci_domain_lock);
  726. clear_bit(zdev->domain, zpci_domain);
  727. spin_unlock(&zpci_domain_lock);
  728. }
  729. int zpci_enable_device(struct zpci_dev *zdev)
  730. {
  731. int rc;
  732. rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  733. if (rc)
  734. goto out;
  735. pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
  736. rc = zpci_dma_init_device(zdev);
  737. if (rc)
  738. goto out_dma;
  739. return 0;
  740. out_dma:
  741. clp_disable_fh(zdev);
  742. out:
  743. return rc;
  744. }
  745. EXPORT_SYMBOL_GPL(zpci_enable_device);
  746. int zpci_disable_device(struct zpci_dev *zdev)
  747. {
  748. zpci_dma_exit_device(zdev);
  749. return clp_disable_fh(zdev);
  750. }
  751. EXPORT_SYMBOL_GPL(zpci_disable_device);
  752. int zpci_create_device(struct zpci_dev *zdev)
  753. {
  754. int rc;
  755. rc = zpci_alloc_domain(zdev);
  756. if (rc)
  757. goto out;
  758. if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
  759. rc = zpci_enable_device(zdev);
  760. if (rc)
  761. goto out_free;
  762. zdev->state = ZPCI_FN_STATE_ONLINE;
  763. }
  764. rc = zpci_scan_bus(zdev);
  765. if (rc)
  766. goto out_disable;
  767. mutex_lock(&zpci_list_lock);
  768. list_add_tail(&zdev->entry, &zpci_list);
  769. if (hotplug_ops)
  770. hotplug_ops->create_slot(zdev);
  771. mutex_unlock(&zpci_list_lock);
  772. return 0;
  773. out_disable:
  774. if (zdev->state == ZPCI_FN_STATE_ONLINE)
  775. zpci_disable_device(zdev);
  776. out_free:
  777. zpci_free_domain(zdev);
  778. out:
  779. return rc;
  780. }
  781. void zpci_stop_device(struct zpci_dev *zdev)
  782. {
  783. zpci_dma_exit_device(zdev);
  784. /*
  785. * Note: SCLP disables fh via set-pci-fn so don't
  786. * do that here.
  787. */
  788. }
  789. EXPORT_SYMBOL_GPL(zpci_stop_device);
  790. static inline int barsize(u8 size)
  791. {
  792. return (size) ? (1 << size) >> 10 : 0;
  793. }
  794. static int zpci_mem_init(void)
  795. {
  796. zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map),
  797. L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL);
  798. if (!zdev_irq_cache)
  799. goto error_zdev;
  800. zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
  801. 16, 0, NULL);
  802. if (!zdev_fmb_cache)
  803. goto error_fmb;
  804. /* TODO: use realloc */
  805. zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
  806. GFP_KERNEL);
  807. if (!zpci_iomap_start)
  808. goto error_iomap;
  809. return 0;
  810. error_iomap:
  811. kmem_cache_destroy(zdev_fmb_cache);
  812. error_fmb:
  813. kmem_cache_destroy(zdev_irq_cache);
  814. error_zdev:
  815. return -ENOMEM;
  816. }
  817. static void zpci_mem_exit(void)
  818. {
  819. kfree(zpci_iomap_start);
  820. kmem_cache_destroy(zdev_irq_cache);
  821. kmem_cache_destroy(zdev_fmb_cache);
  822. }
  823. void zpci_register_hp_ops(struct pci_hp_callback_ops *ops)
  824. {
  825. mutex_lock(&zpci_list_lock);
  826. hotplug_ops = ops;
  827. mutex_unlock(&zpci_list_lock);
  828. }
  829. EXPORT_SYMBOL_GPL(zpci_register_hp_ops);
  830. void zpci_deregister_hp_ops(void)
  831. {
  832. mutex_lock(&zpci_list_lock);
  833. hotplug_ops = NULL;
  834. mutex_unlock(&zpci_list_lock);
  835. }
  836. EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops);
  837. unsigned int s390_pci_probe;
  838. EXPORT_SYMBOL_GPL(s390_pci_probe);
  839. char * __init pcibios_setup(char *str)
  840. {
  841. if (!strcmp(str, "on")) {
  842. s390_pci_probe = 1;
  843. return NULL;
  844. }
  845. return str;
  846. }
  847. static int __init pci_base_init(void)
  848. {
  849. int rc;
  850. if (!s390_pci_probe)
  851. return 0;
  852. if (!test_facility(2) || !test_facility(69)
  853. || !test_facility(71) || !test_facility(72))
  854. return 0;
  855. pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n",
  856. test_facility(69), test_facility(70),
  857. test_facility(71));
  858. rc = zpci_debug_init();
  859. if (rc)
  860. return rc;
  861. rc = zpci_mem_init();
  862. if (rc)
  863. goto out_mem;
  864. rc = zpci_msihash_init();
  865. if (rc)
  866. goto out_hash;
  867. rc = zpci_irq_init();
  868. if (rc)
  869. goto out_irq;
  870. rc = zpci_dma_init();
  871. if (rc)
  872. goto out_dma;
  873. rc = clp_find_pci_devices();
  874. if (rc)
  875. goto out_find;
  876. return 0;
  877. out_find:
  878. zpci_dma_exit();
  879. out_dma:
  880. zpci_irq_exit();
  881. out_irq:
  882. zpci_msihash_exit();
  883. out_hash:
  884. zpci_mem_exit();
  885. out_mem:
  886. zpci_debug_exit();
  887. return rc;
  888. }
  889. subsys_initcall(pci_base_init);