pci.c 24 KB

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