pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/seq_file.h>
  25. #include <linux/pci.h>
  26. #include <linux/msi.h>
  27. #include <asm/facility.h>
  28. #include <asm/pci_insn.h>
  29. #include <asm/pci_clp.h>
  30. #define DEBUG /* enable pr_debug */
  31. #define ZPCI_NR_DMA_SPACES 1
  32. #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
  33. /* list of all detected zpci devices */
  34. LIST_HEAD(zpci_list);
  35. DEFINE_MUTEX(zpci_list_lock);
  36. static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
  37. static DEFINE_SPINLOCK(zpci_domain_lock);
  38. /* I/O Map */
  39. static DEFINE_SPINLOCK(zpci_iomap_lock);
  40. static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  41. struct zpci_iomap_entry *zpci_iomap_start;
  42. EXPORT_SYMBOL_GPL(zpci_iomap_start);
  43. struct zpci_dev *get_zdev(struct pci_dev *pdev)
  44. {
  45. return (struct zpci_dev *) pdev->sysdata;
  46. }
  47. struct zpci_dev *get_zdev_by_fid(u32 fid)
  48. {
  49. struct zpci_dev *tmp, *zdev = NULL;
  50. mutex_lock(&zpci_list_lock);
  51. list_for_each_entry(tmp, &zpci_list, entry) {
  52. if (tmp->fid == fid) {
  53. zdev = tmp;
  54. break;
  55. }
  56. }
  57. mutex_unlock(&zpci_list_lock);
  58. return zdev;
  59. }
  60. bool zpci_fid_present(u32 fid)
  61. {
  62. return (get_zdev_by_fid(fid) != NULL) ? true : false;
  63. }
  64. static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
  65. {
  66. return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
  67. }
  68. int pci_domain_nr(struct pci_bus *bus)
  69. {
  70. return ((struct zpci_dev *) bus->sysdata)->domain;
  71. }
  72. EXPORT_SYMBOL_GPL(pci_domain_nr);
  73. int pci_proc_domain(struct pci_bus *bus)
  74. {
  75. return pci_domain_nr(bus);
  76. }
  77. EXPORT_SYMBOL_GPL(pci_proc_domain);
  78. /* Store PCI function information block */
  79. static int zpci_store_fib(struct zpci_dev *zdev, u8 *fc)
  80. {
  81. struct zpci_fib *fib;
  82. u8 status, cc;
  83. fib = (void *) get_zeroed_page(GFP_KERNEL);
  84. if (!fib)
  85. return -ENOMEM;
  86. do {
  87. cc = __stpcifc(zdev->fh, 0, fib, &status);
  88. if (cc == 2) {
  89. msleep(ZPCI_INSN_BUSY_DELAY);
  90. memset(fib, 0, PAGE_SIZE);
  91. }
  92. } while (cc == 2);
  93. if (cc)
  94. pr_err_once("%s: cc: %u status: %u\n",
  95. __func__, cc, status);
  96. /* Return PCI function controls */
  97. *fc = fib->fc;
  98. free_page((unsigned long) fib);
  99. return (cc) ? -EIO : 0;
  100. }
  101. #define ZPCI_PCIAS_CFGSPC 15
  102. static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
  103. {
  104. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  105. u64 data;
  106. int rc;
  107. rc = pcilg_instr(&data, req, offset);
  108. data = data << ((8 - len) * 8);
  109. data = le64_to_cpu(data);
  110. if (!rc)
  111. *val = (u32) data;
  112. else
  113. *val = 0xffffffff;
  114. return rc;
  115. }
  116. static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
  117. {
  118. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  119. u64 data = val;
  120. int rc;
  121. data = cpu_to_le64(data);
  122. data = data >> ((8 - len) * 8);
  123. rc = pcistg_instr(data, req, offset);
  124. return rc;
  125. }
  126. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  127. {
  128. }
  129. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  130. resource_size_t size,
  131. resource_size_t align)
  132. {
  133. return 0;
  134. }
  135. /* Create a virtual mapping cookie for a PCI BAR */
  136. void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
  137. {
  138. struct zpci_dev *zdev = get_zdev(pdev);
  139. u64 addr;
  140. int idx;
  141. if ((bar & 7) != bar)
  142. return NULL;
  143. idx = zdev->bars[bar].map_idx;
  144. spin_lock(&zpci_iomap_lock);
  145. zpci_iomap_start[idx].fh = zdev->fh;
  146. zpci_iomap_start[idx].bar = bar;
  147. spin_unlock(&zpci_iomap_lock);
  148. addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
  149. return (void __iomem *) addr;
  150. }
  151. EXPORT_SYMBOL_GPL(pci_iomap);
  152. void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
  153. {
  154. unsigned int idx;
  155. idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
  156. spin_lock(&zpci_iomap_lock);
  157. zpci_iomap_start[idx].fh = 0;
  158. zpci_iomap_start[idx].bar = 0;
  159. spin_unlock(&zpci_iomap_lock);
  160. }
  161. EXPORT_SYMBOL_GPL(pci_iounmap);
  162. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  163. int size, u32 *val)
  164. {
  165. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  166. if (!zdev || devfn != ZPCI_DEVFN)
  167. return 0;
  168. return zpci_cfg_load(zdev, where, val, size);
  169. }
  170. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  171. int size, u32 val)
  172. {
  173. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  174. if (!zdev || devfn != ZPCI_DEVFN)
  175. return 0;
  176. return zpci_cfg_store(zdev, where, val, size);
  177. }
  178. static struct pci_ops pci_root_ops = {
  179. .read = pci_read,
  180. .write = pci_write,
  181. };
  182. static void zpci_map_resources(struct zpci_dev *zdev)
  183. {
  184. struct pci_dev *pdev = zdev->pdev;
  185. resource_size_t len;
  186. int i;
  187. for (i = 0; i < PCI_BAR_COUNT; i++) {
  188. len = pci_resource_len(pdev, i);
  189. if (!len)
  190. continue;
  191. pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
  192. pdev->resource[i].end = pdev->resource[i].start + len - 1;
  193. pr_debug("BAR%i: -> start: %Lx end: %Lx\n",
  194. i, pdev->resource[i].start, pdev->resource[i].end);
  195. }
  196. };
  197. static void zpci_unmap_resources(struct pci_dev *pdev)
  198. {
  199. resource_size_t len;
  200. int i;
  201. for (i = 0; i < PCI_BAR_COUNT; i++) {
  202. len = pci_resource_len(pdev, i);
  203. if (!len)
  204. continue;
  205. pci_iounmap(pdev, (void *) pdev->resource[i].start);
  206. }
  207. };
  208. struct zpci_dev *zpci_alloc_device(void)
  209. {
  210. struct zpci_dev *zdev;
  211. /* Alloc memory for our private pci device data */
  212. zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
  213. if (!zdev)
  214. return ERR_PTR(-ENOMEM);
  215. return zdev;
  216. }
  217. void zpci_free_device(struct zpci_dev *zdev)
  218. {
  219. kfree(zdev);
  220. }
  221. /* Called on removal of pci_dev, leaves zpci and bus device */
  222. static void zpci_remove_device(struct pci_dev *pdev)
  223. {
  224. struct zpci_dev *zdev = get_zdev(pdev);
  225. dev_info(&pdev->dev, "Removing device %u\n", zdev->domain);
  226. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  227. zpci_unmap_resources(pdev);
  228. list_del(&zdev->entry); /* can be called from init */
  229. zdev->pdev = NULL;
  230. }
  231. static void zpci_scan_devices(void)
  232. {
  233. struct zpci_dev *zdev;
  234. mutex_lock(&zpci_list_lock);
  235. list_for_each_entry(zdev, &zpci_list, entry)
  236. if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
  237. zpci_scan_device(zdev);
  238. mutex_unlock(&zpci_list_lock);
  239. }
  240. /*
  241. * Too late for any s390 specific setup, since interrupts must be set up
  242. * already which requires DMA setup too and the pci scan will access the
  243. * config space, which only works if the function handle is enabled.
  244. */
  245. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  246. {
  247. struct resource *res;
  248. u16 cmd;
  249. int i;
  250. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  251. for (i = 0; i < PCI_BAR_COUNT; i++) {
  252. res = &pdev->resource[i];
  253. if (res->flags & IORESOURCE_IO)
  254. return -EINVAL;
  255. if (res->flags & IORESOURCE_MEM)
  256. cmd |= PCI_COMMAND_MEMORY;
  257. }
  258. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  259. return 0;
  260. }
  261. void pcibios_disable_device(struct pci_dev *pdev)
  262. {
  263. zpci_remove_device(pdev);
  264. pdev->sysdata = NULL;
  265. }
  266. static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
  267. unsigned long flags, int domain)
  268. {
  269. struct resource *r;
  270. char *name;
  271. int rc;
  272. r = kzalloc(sizeof(*r), GFP_KERNEL);
  273. if (!r)
  274. return ERR_PTR(-ENOMEM);
  275. r->start = start;
  276. r->end = r->start + size - 1;
  277. r->flags = flags;
  278. r->parent = &iomem_resource;
  279. name = kmalloc(18, GFP_KERNEL);
  280. if (!name) {
  281. kfree(r);
  282. return ERR_PTR(-ENOMEM);
  283. }
  284. sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
  285. r->name = name;
  286. rc = request_resource(&iomem_resource, r);
  287. if (rc)
  288. pr_debug("request resource %pR failed\n", r);
  289. return r;
  290. }
  291. static int zpci_alloc_iomap(struct zpci_dev *zdev)
  292. {
  293. int entry;
  294. spin_lock(&zpci_iomap_lock);
  295. entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  296. if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
  297. spin_unlock(&zpci_iomap_lock);
  298. return -ENOSPC;
  299. }
  300. set_bit(entry, zpci_iomap);
  301. spin_unlock(&zpci_iomap_lock);
  302. return entry;
  303. }
  304. static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
  305. {
  306. spin_lock(&zpci_iomap_lock);
  307. memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
  308. clear_bit(entry, zpci_iomap);
  309. spin_unlock(&zpci_iomap_lock);
  310. }
  311. static int zpci_create_device_bus(struct zpci_dev *zdev)
  312. {
  313. struct resource *res;
  314. LIST_HEAD(resources);
  315. int i;
  316. /* allocate mapping entry for each used bar */
  317. for (i = 0; i < PCI_BAR_COUNT; i++) {
  318. unsigned long addr, size, flags;
  319. int entry;
  320. if (!zdev->bars[i].size)
  321. continue;
  322. entry = zpci_alloc_iomap(zdev);
  323. if (entry < 0)
  324. return entry;
  325. zdev->bars[i].map_idx = entry;
  326. /* only MMIO is supported */
  327. flags = IORESOURCE_MEM;
  328. if (zdev->bars[i].val & 8)
  329. flags |= IORESOURCE_PREFETCH;
  330. if (zdev->bars[i].val & 4)
  331. flags |= IORESOURCE_MEM_64;
  332. addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
  333. size = 1UL << zdev->bars[i].size;
  334. res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
  335. if (IS_ERR(res)) {
  336. zpci_free_iomap(zdev, entry);
  337. return PTR_ERR(res);
  338. }
  339. pci_add_resource(&resources, res);
  340. }
  341. zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
  342. zdev, &resources);
  343. if (!zdev->bus)
  344. return -EIO;
  345. zdev->bus->max_bus_speed = zdev->max_bus_speed;
  346. return 0;
  347. }
  348. static int zpci_alloc_domain(struct zpci_dev *zdev)
  349. {
  350. spin_lock(&zpci_domain_lock);
  351. zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
  352. if (zdev->domain == ZPCI_NR_DEVICES) {
  353. spin_unlock(&zpci_domain_lock);
  354. return -ENOSPC;
  355. }
  356. set_bit(zdev->domain, zpci_domain);
  357. spin_unlock(&zpci_domain_lock);
  358. return 0;
  359. }
  360. static void zpci_free_domain(struct zpci_dev *zdev)
  361. {
  362. spin_lock(&zpci_domain_lock);
  363. clear_bit(zdev->domain, zpci_domain);
  364. spin_unlock(&zpci_domain_lock);
  365. }
  366. int zpci_enable_device(struct zpci_dev *zdev)
  367. {
  368. int rc;
  369. rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  370. if (rc)
  371. goto out;
  372. pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
  373. return 0;
  374. out:
  375. return rc;
  376. }
  377. EXPORT_SYMBOL_GPL(zpci_enable_device);
  378. int zpci_create_device(struct zpci_dev *zdev)
  379. {
  380. int rc;
  381. rc = zpci_alloc_domain(zdev);
  382. if (rc)
  383. goto out;
  384. rc = zpci_create_device_bus(zdev);
  385. if (rc)
  386. goto out_bus;
  387. mutex_lock(&zpci_list_lock);
  388. list_add_tail(&zdev->entry, &zpci_list);
  389. mutex_unlock(&zpci_list_lock);
  390. if (zdev->state == ZPCI_FN_STATE_STANDBY)
  391. return 0;
  392. rc = zpci_enable_device(zdev);
  393. if (rc)
  394. goto out_start;
  395. return 0;
  396. out_start:
  397. mutex_lock(&zpci_list_lock);
  398. list_del(&zdev->entry);
  399. mutex_unlock(&zpci_list_lock);
  400. out_bus:
  401. zpci_free_domain(zdev);
  402. out:
  403. return rc;
  404. }
  405. void zpci_stop_device(struct zpci_dev *zdev)
  406. {
  407. /*
  408. * Note: SCLP disables fh via set-pci-fn so don't
  409. * do that here.
  410. */
  411. }
  412. EXPORT_SYMBOL_GPL(zpci_stop_device);
  413. int zpci_scan_device(struct zpci_dev *zdev)
  414. {
  415. zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN);
  416. if (!zdev->pdev) {
  417. pr_err("pci_scan_single_device failed for fid: 0x%x\n",
  418. zdev->fid);
  419. goto out;
  420. }
  421. zpci_map_resources(zdev);
  422. pci_bus_add_devices(zdev->bus);
  423. /* now that pdev was added to the bus mark it as used */
  424. zdev->state = ZPCI_FN_STATE_ONLINE;
  425. return 0;
  426. out:
  427. clp_disable_fh(zdev);
  428. return -EIO;
  429. }
  430. EXPORT_SYMBOL_GPL(zpci_scan_device);
  431. static inline int barsize(u8 size)
  432. {
  433. return (size) ? (1 << size) >> 10 : 0;
  434. }
  435. static int zpci_mem_init(void)
  436. {
  437. /* TODO: use realloc */
  438. zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
  439. GFP_KERNEL);
  440. if (!zpci_iomap_start)
  441. goto error_zdev;
  442. return 0;
  443. error_zdev:
  444. return -ENOMEM;
  445. }
  446. static void zpci_mem_exit(void)
  447. {
  448. kfree(zpci_iomap_start);
  449. }
  450. unsigned int pci_probe = 1;
  451. EXPORT_SYMBOL_GPL(pci_probe);
  452. char * __init pcibios_setup(char *str)
  453. {
  454. if (!strcmp(str, "off")) {
  455. pci_probe = 0;
  456. return NULL;
  457. }
  458. return str;
  459. }
  460. static int __init pci_base_init(void)
  461. {
  462. int rc;
  463. if (!pci_probe)
  464. return 0;
  465. if (!test_facility(2) || !test_facility(69)
  466. || !test_facility(71) || !test_facility(72))
  467. return 0;
  468. pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n",
  469. test_facility(69), test_facility(70),
  470. test_facility(71));
  471. rc = zpci_mem_init();
  472. if (rc)
  473. goto out_mem;
  474. rc = clp_find_pci_devices();
  475. if (rc)
  476. goto out_find;
  477. zpci_scan_devices();
  478. return 0;
  479. out_find:
  480. zpci_mem_exit();
  481. out_mem:
  482. return rc;
  483. }
  484. subsys_initcall(pci_base_init);