pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/delay.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/capability.h>
  20. #include <linux/sched.h>
  21. #include <linux/errno.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/export.h>
  26. #include <asm/processor.h>
  27. #include <asm/sections.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/hv_driver.h>
  30. #include <hv/drv_pcie_rc_intf.h>
  31. /*
  32. * Initialization flow and process
  33. * -------------------------------
  34. *
  35. * This files contains the routines to search for PCI buses,
  36. * enumerate the buses, and configure any attached devices.
  37. *
  38. * There are two entry points here:
  39. * 1) tile_pci_init
  40. * This sets up the pci_controller structs, and opens the
  41. * FDs to the hypervisor. This is called from setup_arch() early
  42. * in the boot process.
  43. * 2) pcibios_init
  44. * This probes the PCI bus(es) for any attached hardware. It's
  45. * called by subsys_initcall. All of the real work is done by the
  46. * generic Linux PCI layer.
  47. *
  48. */
  49. static int pci_probe = 1;
  50. /*
  51. * This flag tells if the platform is TILEmpower that needs
  52. * special configuration for the PLX switch chip.
  53. */
  54. int __ro_after_init tile_plx_gen1;
  55. static struct pci_controller controllers[TILE_NUM_PCIE];
  56. static int num_controllers;
  57. static int pci_scan_flags[TILE_NUM_PCIE];
  58. static struct pci_ops tile_cfg_ops;
  59. /*
  60. * Open a FD to the hypervisor PCI device.
  61. *
  62. * controller_id is the controller number, config type is 0 or 1 for
  63. * config0 or config1 operations.
  64. */
  65. static int tile_pcie_open(int controller_id, int config_type)
  66. {
  67. char filename[32];
  68. int fd;
  69. sprintf(filename, "pcie/%d/config%d", controller_id, config_type);
  70. fd = hv_dev_open((HV_VirtAddr)filename, 0);
  71. return fd;
  72. }
  73. /*
  74. * Get the IRQ numbers from the HV and set up the handlers for them.
  75. */
  76. static int tile_init_irqs(int controller_id, struct pci_controller *controller)
  77. {
  78. char filename[32];
  79. int fd;
  80. int ret;
  81. int x;
  82. struct pcie_rc_config rc_config;
  83. sprintf(filename, "pcie/%d/ctl", controller_id);
  84. fd = hv_dev_open((HV_VirtAddr)filename, 0);
  85. if (fd < 0) {
  86. pr_err("PCI: hv_dev_open(%s) failed\n", filename);
  87. return -1;
  88. }
  89. ret = hv_dev_pread(fd, 0, (HV_VirtAddr)(&rc_config),
  90. sizeof(rc_config), PCIE_RC_CONFIG_MASK_OFF);
  91. hv_dev_close(fd);
  92. if (ret != sizeof(rc_config)) {
  93. pr_err("PCI: wanted %zd bytes, got %d\n",
  94. sizeof(rc_config), ret);
  95. return -1;
  96. }
  97. /* Record irq_base so that we can map INTx to IRQ # later. */
  98. controller->irq_base = rc_config.intr;
  99. for (x = 0; x < 4; x++)
  100. tile_irq_activate(rc_config.intr + x,
  101. TILE_IRQ_HW_CLEAR);
  102. if (rc_config.plx_gen1)
  103. controller->plx_gen1 = 1;
  104. return 0;
  105. }
  106. /*
  107. * First initialization entry point, called from setup_arch().
  108. *
  109. * Find valid controllers and fill in pci_controller structs for each
  110. * of them.
  111. *
  112. * Returns the number of controllers discovered.
  113. */
  114. int __init tile_pci_init(void)
  115. {
  116. int i;
  117. if (!pci_probe) {
  118. pr_info("PCI: disabled by boot argument\n");
  119. return 0;
  120. }
  121. pr_info("PCI: Searching for controllers...\n");
  122. /* Re-init number of PCIe controllers to support hot-plug feature. */
  123. num_controllers = 0;
  124. /* Do any configuration we need before using the PCIe */
  125. for (i = 0; i < TILE_NUM_PCIE; i++) {
  126. /*
  127. * To see whether we need a real config op based on
  128. * the results of pcibios_init(), to support PCIe hot-plug.
  129. */
  130. if (pci_scan_flags[i] == 0) {
  131. int hv_cfg_fd0 = -1;
  132. int hv_cfg_fd1 = -1;
  133. int hv_mem_fd = -1;
  134. char name[32];
  135. struct pci_controller *controller;
  136. /*
  137. * Open the fd to the HV. If it fails then this
  138. * device doesn't exist.
  139. */
  140. hv_cfg_fd0 = tile_pcie_open(i, 0);
  141. if (hv_cfg_fd0 < 0)
  142. continue;
  143. hv_cfg_fd1 = tile_pcie_open(i, 1);
  144. if (hv_cfg_fd1 < 0) {
  145. pr_err("PCI: Couldn't open config fd to HV for controller %d\n",
  146. i);
  147. goto err_cont;
  148. }
  149. sprintf(name, "pcie/%d/mem", i);
  150. hv_mem_fd = hv_dev_open((HV_VirtAddr)name, 0);
  151. if (hv_mem_fd < 0) {
  152. pr_err("PCI: Could not open mem fd to HV!\n");
  153. goto err_cont;
  154. }
  155. pr_info("PCI: Found PCI controller #%d\n", i);
  156. controller = &controllers[i];
  157. controller->index = i;
  158. controller->hv_cfg_fd[0] = hv_cfg_fd0;
  159. controller->hv_cfg_fd[1] = hv_cfg_fd1;
  160. controller->hv_mem_fd = hv_mem_fd;
  161. controller->last_busno = 0xff;
  162. controller->ops = &tile_cfg_ops;
  163. num_controllers++;
  164. continue;
  165. err_cont:
  166. if (hv_cfg_fd0 >= 0)
  167. hv_dev_close(hv_cfg_fd0);
  168. if (hv_cfg_fd1 >= 0)
  169. hv_dev_close(hv_cfg_fd1);
  170. if (hv_mem_fd >= 0)
  171. hv_dev_close(hv_mem_fd);
  172. continue;
  173. }
  174. }
  175. /*
  176. * Before using the PCIe, see if we need to do any platform-specific
  177. * configuration, such as the PLX switch Gen 1 issue on TILEmpower.
  178. */
  179. for (i = 0; i < num_controllers; i++) {
  180. struct pci_controller *controller = &controllers[i];
  181. if (controller->plx_gen1)
  182. tile_plx_gen1 = 1;
  183. }
  184. return num_controllers;
  185. }
  186. /*
  187. * (pin - 1) converts from the PCI standard's [1:4] convention to
  188. * a normal [0:3] range.
  189. */
  190. static int tile_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  191. {
  192. struct pci_controller *controller =
  193. (struct pci_controller *)dev->sysdata;
  194. return (pin - 1) + controller->irq_base;
  195. }
  196. static void fixup_read_and_payload_sizes(void)
  197. {
  198. struct pci_dev *dev = NULL;
  199. int smallest_max_payload = 0x1; /* Tile maxes out at 256 bytes. */
  200. int max_read_size = PCI_EXP_DEVCTL_READRQ_512B;
  201. u16 new_values;
  202. /* Scan for the smallest maximum payload size. */
  203. for_each_pci_dev(dev) {
  204. if (!pci_is_pcie(dev))
  205. continue;
  206. if (dev->pcie_mpss < smallest_max_payload)
  207. smallest_max_payload = dev->pcie_mpss;
  208. }
  209. /* Now, set the max_payload_size for all devices to that value. */
  210. new_values = max_read_size | (smallest_max_payload << 5);
  211. for_each_pci_dev(dev)
  212. pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  213. PCI_EXP_DEVCTL_PAYLOAD | PCI_EXP_DEVCTL_READRQ,
  214. new_values);
  215. }
  216. /*
  217. * Second PCI initialization entry point, called by subsys_initcall.
  218. *
  219. * The controllers have been set up by the time we get here, by a call to
  220. * tile_pci_init.
  221. */
  222. int __init pcibios_init(void)
  223. {
  224. struct pci_host_bridge *bridge;
  225. int i;
  226. pr_info("PCI: Probing PCI hardware\n");
  227. /*
  228. * Delay a bit in case devices aren't ready. Some devices are
  229. * known to require at least 20ms here, but we use a more
  230. * conservative value.
  231. */
  232. msleep(250);
  233. /* Scan all of the recorded PCI controllers. */
  234. for (i = 0; i < TILE_NUM_PCIE; i++) {
  235. /*
  236. * Do real pcibios init ops if the controller is initialized
  237. * by tile_pci_init() successfully and not initialized by
  238. * pcibios_init() yet to support PCIe hot-plug.
  239. */
  240. if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
  241. struct pci_controller *controller = &controllers[i];
  242. struct pci_bus *bus;
  243. LIST_HEAD(resources);
  244. if (tile_init_irqs(i, controller)) {
  245. pr_err("PCI: Could not initialize IRQs\n");
  246. continue;
  247. }
  248. pr_info("PCI: initializing controller #%d\n", i);
  249. pci_add_resource(&resources, &ioport_resource);
  250. pci_add_resource(&resources, &iomem_resource);
  251. bridge = pci_alloc_host_bridge(0);
  252. if (!bridge)
  253. break;
  254. list_splice_init(&resources, &bridge->windows);
  255. bridge->dev.parent = NULL;
  256. bridge->sysdata = controller;
  257. bridge->busnr = 0;
  258. bridge->ops = controller->ops;
  259. bridge->swizzle_irq = pci_common_swizzle;
  260. bridge->map_irq = tile_map_irq;
  261. pci_scan_root_bus_bridge(bridge);
  262. bus = bridge->bus;
  263. controller->root_bus = bus;
  264. controller->last_busno = bus->busn_res.end;
  265. }
  266. }
  267. /*
  268. * This comes from the generic Linux PCI driver.
  269. *
  270. * It allocates all of the resources (I/O memory, etc)
  271. * associated with the devices read in above.
  272. */
  273. pci_assign_unassigned_resources();
  274. /* Configure the max_read_size and max_payload_size values. */
  275. fixup_read_and_payload_sizes();
  276. /* Record the I/O resources in the PCI controller structure. */
  277. for (i = 0; i < TILE_NUM_PCIE; i++) {
  278. /*
  279. * Do real pcibios init ops if the controller is initialized
  280. * by tile_pci_init() successfully and not initialized by
  281. * pcibios_init() yet to support PCIe hot-plug.
  282. */
  283. if (pci_scan_flags[i] == 0 && controllers[i].ops != NULL) {
  284. struct pci_bus *root_bus = controllers[i].root_bus;
  285. struct pci_bus *next_bus;
  286. struct pci_dev *dev;
  287. pci_bus_add_devices(root_bus);
  288. list_for_each_entry(dev, &root_bus->devices, bus_list) {
  289. /*
  290. * Find the PCI host controller, ie. the 1st
  291. * bridge.
  292. */
  293. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI &&
  294. (PCI_SLOT(dev->devfn) == 0)) {
  295. next_bus = dev->subordinate;
  296. controllers[i].mem_resources[0] =
  297. *next_bus->resource[0];
  298. controllers[i].mem_resources[1] =
  299. *next_bus->resource[1];
  300. controllers[i].mem_resources[2] =
  301. *next_bus->resource[2];
  302. /* Setup flags. */
  303. pci_scan_flags[i] = 1;
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. return 0;
  310. }
  311. subsys_initcall(pcibios_init);
  312. void pcibios_set_master(struct pci_dev *dev)
  313. {
  314. /* No special bus mastering setup handling. */
  315. }
  316. /* Process any "pci=" kernel boot arguments. */
  317. char *__init pcibios_setup(char *str)
  318. {
  319. if (!strcmp(str, "off")) {
  320. pci_probe = 0;
  321. return NULL;
  322. }
  323. return str;
  324. }
  325. /*
  326. * Enable memory and/or address decoding, as appropriate, for the
  327. * device described by the 'dev' struct.
  328. *
  329. * This is called from the generic PCI layer, and can be called
  330. * for bridges or endpoints.
  331. */
  332. int pcibios_enable_device(struct pci_dev *dev, int mask)
  333. {
  334. u16 cmd, old_cmd;
  335. u8 header_type;
  336. int i;
  337. struct resource *r;
  338. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  339. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  340. old_cmd = cmd;
  341. if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
  342. /*
  343. * For bridges, we enable both memory and I/O decoding
  344. * in call cases.
  345. */
  346. cmd |= PCI_COMMAND_IO;
  347. cmd |= PCI_COMMAND_MEMORY;
  348. } else {
  349. /*
  350. * For endpoints, we enable memory and/or I/O decoding
  351. * only if they have a memory resource of that type.
  352. */
  353. for (i = 0; i < 6; i++) {
  354. r = &dev->resource[i];
  355. if (r->flags & IORESOURCE_UNSET) {
  356. pr_err("PCI: Device %s not available because of resource collisions\n",
  357. pci_name(dev));
  358. return -EINVAL;
  359. }
  360. if (r->flags & IORESOURCE_IO)
  361. cmd |= PCI_COMMAND_IO;
  362. if (r->flags & IORESOURCE_MEM)
  363. cmd |= PCI_COMMAND_MEMORY;
  364. }
  365. }
  366. /*
  367. * We only write the command if it changed.
  368. */
  369. if (cmd != old_cmd)
  370. pci_write_config_word(dev, PCI_COMMAND, cmd);
  371. return 0;
  372. }
  373. /****************************************************************
  374. *
  375. * Tile PCI config space read/write routines
  376. *
  377. ****************************************************************/
  378. /*
  379. * These are the normal read and write ops
  380. * These are expanded with macros from pci_bus_read_config_byte() etc.
  381. *
  382. * devfn is the combined PCI slot & function.
  383. *
  384. * offset is in bytes, from the start of config space for the
  385. * specified bus & slot.
  386. */
  387. static int tile_cfg_read(struct pci_bus *bus, unsigned int devfn, int offset,
  388. int size, u32 *val)
  389. {
  390. struct pci_controller *controller = bus->sysdata;
  391. int busnum = bus->number & 0xff;
  392. int slot = (devfn >> 3) & 0x1f;
  393. int function = devfn & 0x7;
  394. u32 addr;
  395. int config_mode = 1;
  396. /*
  397. * There is no bridge between the Tile and bus 0, so we
  398. * use config0 to talk to bus 0.
  399. *
  400. * If we're talking to a bus other than zero then we
  401. * must have found a bridge.
  402. */
  403. if (busnum == 0) {
  404. /*
  405. * We fake an empty slot for (busnum == 0) && (slot > 0),
  406. * since there is only one slot on bus 0.
  407. */
  408. if (slot) {
  409. *val = 0xFFFFFFFF;
  410. return 0;
  411. }
  412. config_mode = 0;
  413. }
  414. addr = busnum << 20; /* Bus in 27:20 */
  415. addr |= slot << 15; /* Slot (device) in 19:15 */
  416. addr |= function << 12; /* Function is in 14:12 */
  417. addr |= (offset & 0xFFF); /* byte address in 0:11 */
  418. return hv_dev_pread(controller->hv_cfg_fd[config_mode], 0,
  419. (HV_VirtAddr)(val), size, addr);
  420. }
  421. /*
  422. * See tile_cfg_read() for relevant comments.
  423. * Note that "val" is the value to write, not a pointer to that value.
  424. */
  425. static int tile_cfg_write(struct pci_bus *bus, unsigned int devfn, int offset,
  426. int size, u32 val)
  427. {
  428. struct pci_controller *controller = bus->sysdata;
  429. int busnum = bus->number & 0xff;
  430. int slot = (devfn >> 3) & 0x1f;
  431. int function = devfn & 0x7;
  432. u32 addr;
  433. int config_mode = 1;
  434. HV_VirtAddr valp = (HV_VirtAddr)&val;
  435. /*
  436. * For bus 0 slot 0 we use config 0 accesses.
  437. */
  438. if (busnum == 0) {
  439. /*
  440. * We fake an empty slot for (busnum == 0) && (slot > 0),
  441. * since there is only one slot on bus 0.
  442. */
  443. if (slot)
  444. return 0;
  445. config_mode = 0;
  446. }
  447. addr = busnum << 20; /* Bus in 27:20 */
  448. addr |= slot << 15; /* Slot (device) in 19:15 */
  449. addr |= function << 12; /* Function is in 14:12 */
  450. addr |= (offset & 0xFFF); /* byte address in 0:11 */
  451. #ifdef __BIG_ENDIAN
  452. /* Point to the correct part of the 32-bit "val". */
  453. valp += 4 - size;
  454. #endif
  455. return hv_dev_pwrite(controller->hv_cfg_fd[config_mode], 0,
  456. valp, size, addr);
  457. }
  458. static struct pci_ops tile_cfg_ops = {
  459. .read = tile_cfg_read,
  460. .write = tile_cfg_write,
  461. };
  462. /*
  463. * In the following, each PCI controller's mem_resources[1]
  464. * represents its (non-prefetchable) PCI memory resource.
  465. * mem_resources[0] and mem_resources[2] refer to its PCI I/O and
  466. * prefetchable PCI memory resources, respectively.
  467. * For more details, see pci_setup_bridge() in setup-bus.c.
  468. * By comparing the target PCI memory address against the
  469. * end address of controller 0, we can determine the controller
  470. * that should accept the PCI memory access.
  471. */
  472. #define TILE_READ(size, type) \
  473. type _tile_read##size(unsigned long addr) \
  474. { \
  475. type val; \
  476. int idx = 0; \
  477. if (addr > controllers[0].mem_resources[1].end && \
  478. addr > controllers[0].mem_resources[2].end) \
  479. idx = 1; \
  480. if (hv_dev_pread(controllers[idx].hv_mem_fd, 0, \
  481. (HV_VirtAddr)(&val), sizeof(type), addr)) \
  482. pr_err("PCI: read %zd bytes at 0x%lX failed\n", \
  483. sizeof(type), addr); \
  484. return val; \
  485. } \
  486. EXPORT_SYMBOL(_tile_read##size)
  487. TILE_READ(b, u8);
  488. TILE_READ(w, u16);
  489. TILE_READ(l, u32);
  490. TILE_READ(q, u64);
  491. #define TILE_WRITE(size, type) \
  492. void _tile_write##size(type val, unsigned long addr) \
  493. { \
  494. int idx = 0; \
  495. if (addr > controllers[0].mem_resources[1].end && \
  496. addr > controllers[0].mem_resources[2].end) \
  497. idx = 1; \
  498. if (hv_dev_pwrite(controllers[idx].hv_mem_fd, 0, \
  499. (HV_VirtAddr)(&val), sizeof(type), addr)) \
  500. pr_err("PCI: write %zd bytes at 0x%lX failed\n", \
  501. sizeof(type), addr); \
  502. } \
  503. EXPORT_SYMBOL(_tile_write##size)
  504. TILE_WRITE(b, u8);
  505. TILE_WRITE(w, u16);
  506. TILE_WRITE(l, u32);
  507. TILE_WRITE(q, u64);