conf_space_header.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Backend - Handles the virtual fields in the configuration space headers.
  4. *
  5. * Author: Ryan Wilson <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #define dev_fmt pr_fmt
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include "pciback.h"
  12. #include "conf_space.h"
  13. struct pci_cmd_info {
  14. u16 val;
  15. };
  16. struct pci_bar_info {
  17. u32 val;
  18. u32 len_val;
  19. int which;
  20. };
  21. #define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO))
  22. #define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER)
  23. /* Bits guests are allowed to control in permissive mode. */
  24. #define PCI_COMMAND_GUEST (PCI_COMMAND_MASTER|PCI_COMMAND_SPECIAL| \
  25. PCI_COMMAND_INVALIDATE|PCI_COMMAND_VGA_PALETTE| \
  26. PCI_COMMAND_WAIT|PCI_COMMAND_FAST_BACK)
  27. static void *command_init(struct pci_dev *dev, int offset)
  28. {
  29. struct pci_cmd_info *cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  30. int err;
  31. if (!cmd)
  32. return ERR_PTR(-ENOMEM);
  33. err = pci_read_config_word(dev, PCI_COMMAND, &cmd->val);
  34. if (err) {
  35. kfree(cmd);
  36. return ERR_PTR(err);
  37. }
  38. return cmd;
  39. }
  40. static int command_read(struct pci_dev *dev, int offset, u16 *value, void *data)
  41. {
  42. int ret = pci_read_config_word(dev, offset, value);
  43. const struct pci_cmd_info *cmd = data;
  44. *value &= PCI_COMMAND_GUEST;
  45. *value |= cmd->val & ~PCI_COMMAND_GUEST;
  46. return ret;
  47. }
  48. static int command_write(struct pci_dev *dev, int offset, u16 value, void *data)
  49. {
  50. struct xen_pcibk_dev_data *dev_data;
  51. int err;
  52. u16 val;
  53. struct pci_cmd_info *cmd = data;
  54. dev_data = pci_get_drvdata(dev);
  55. if (!pci_is_enabled(dev) && is_enable_cmd(value)) {
  56. dev_dbg(&dev->dev, "enable\n");
  57. err = pci_enable_device(dev);
  58. if (err)
  59. return err;
  60. if (dev_data)
  61. dev_data->enable_intx = 1;
  62. } else if (pci_is_enabled(dev) && !is_enable_cmd(value)) {
  63. dev_dbg(&dev->dev, "disable\n");
  64. pci_disable_device(dev);
  65. if (dev_data)
  66. dev_data->enable_intx = 0;
  67. }
  68. if (!dev->is_busmaster && is_master_cmd(value)) {
  69. dev_dbg(&dev->dev, "set bus master\n");
  70. pci_set_master(dev);
  71. } else if (dev->is_busmaster && !is_master_cmd(value)) {
  72. dev_dbg(&dev->dev, "clear bus master\n");
  73. pci_clear_master(dev);
  74. }
  75. if (!(cmd->val & PCI_COMMAND_INVALIDATE) &&
  76. (value & PCI_COMMAND_INVALIDATE)) {
  77. dev_dbg(&dev->dev, "enable memory-write-invalidate\n");
  78. err = pci_set_mwi(dev);
  79. if (err) {
  80. dev_warn(&dev->dev, "cannot enable memory-write-invalidate (%d)\n",
  81. err);
  82. value &= ~PCI_COMMAND_INVALIDATE;
  83. }
  84. } else if ((cmd->val & PCI_COMMAND_INVALIDATE) &&
  85. !(value & PCI_COMMAND_INVALIDATE)) {
  86. dev_dbg(&dev->dev, "disable memory-write-invalidate\n");
  87. pci_clear_mwi(dev);
  88. }
  89. if (dev_data && dev_data->allow_interrupt_control &&
  90. ((cmd->val ^ value) & PCI_COMMAND_INTX_DISABLE))
  91. pci_intx(dev, !(value & PCI_COMMAND_INTX_DISABLE));
  92. cmd->val = value;
  93. if (!xen_pcibk_permissive && (!dev_data || !dev_data->permissive))
  94. return 0;
  95. /* Only allow the guest to control certain bits. */
  96. err = pci_read_config_word(dev, offset, &val);
  97. if (err || val == value)
  98. return err;
  99. value &= PCI_COMMAND_GUEST;
  100. value |= val & ~PCI_COMMAND_GUEST;
  101. return pci_write_config_word(dev, offset, value);
  102. }
  103. static int rom_write(struct pci_dev *dev, int offset, u32 value, void *data)
  104. {
  105. struct pci_bar_info *bar = data;
  106. if (unlikely(!bar)) {
  107. dev_warn(&dev->dev, "driver data not found\n");
  108. return XEN_PCI_ERR_op_failed;
  109. }
  110. /* A write to obtain the length must happen as a 32-bit write.
  111. * This does not (yet) support writing individual bytes
  112. */
  113. if ((value | ~PCI_ROM_ADDRESS_MASK) == ~0U)
  114. bar->which = 1;
  115. else {
  116. u32 tmpval;
  117. pci_read_config_dword(dev, offset, &tmpval);
  118. if (tmpval != bar->val && value == bar->val) {
  119. /* Allow restoration of bar value. */
  120. pci_write_config_dword(dev, offset, bar->val);
  121. }
  122. bar->which = 0;
  123. }
  124. /* Do we need to support enabling/disabling the rom address here? */
  125. return 0;
  126. }
  127. /* For the BARs, only allow writes which write ~0 or
  128. * the correct resource information
  129. * (Needed for when the driver probes the resource usage)
  130. */
  131. static int bar_write(struct pci_dev *dev, int offset, u32 value, void *data)
  132. {
  133. struct pci_bar_info *bar = data;
  134. unsigned int pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  135. const struct resource *res = dev->resource;
  136. u32 mask;
  137. if (unlikely(!bar)) {
  138. dev_warn(&dev->dev, "driver data not found\n");
  139. return XEN_PCI_ERR_op_failed;
  140. }
  141. /* A write to obtain the length must happen as a 32-bit write.
  142. * This does not (yet) support writing individual bytes
  143. */
  144. if (res[pos].flags & IORESOURCE_IO)
  145. mask = ~PCI_BASE_ADDRESS_IO_MASK;
  146. else if (pos && (res[pos - 1].flags & IORESOURCE_MEM_64))
  147. mask = 0;
  148. else
  149. mask = ~PCI_BASE_ADDRESS_MEM_MASK;
  150. if ((value | mask) == ~0U)
  151. bar->which = 1;
  152. else {
  153. u32 tmpval;
  154. pci_read_config_dword(dev, offset, &tmpval);
  155. if (tmpval != bar->val && value == bar->val) {
  156. /* Allow restoration of bar value. */
  157. pci_write_config_dword(dev, offset, bar->val);
  158. }
  159. bar->which = 0;
  160. }
  161. return 0;
  162. }
  163. static int bar_read(struct pci_dev *dev, int offset, u32 * value, void *data)
  164. {
  165. struct pci_bar_info *bar = data;
  166. if (unlikely(!bar)) {
  167. dev_warn(&dev->dev, "driver data not found\n");
  168. return XEN_PCI_ERR_op_failed;
  169. }
  170. *value = bar->which ? bar->len_val : bar->val;
  171. return 0;
  172. }
  173. static void *bar_init(struct pci_dev *dev, int offset)
  174. {
  175. unsigned int pos;
  176. const struct resource *res = dev->resource;
  177. struct pci_bar_info *bar = kzalloc(sizeof(*bar), GFP_KERNEL);
  178. if (!bar)
  179. return ERR_PTR(-ENOMEM);
  180. if (offset == PCI_ROM_ADDRESS || offset == PCI_ROM_ADDRESS1)
  181. pos = PCI_ROM_RESOURCE;
  182. else {
  183. pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  184. if (pos && (res[pos - 1].flags & IORESOURCE_MEM_64)) {
  185. /*
  186. * Use ">> 16 >> 16" instead of direct ">> 32" shift
  187. * to avoid warnings on 32-bit architectures.
  188. */
  189. bar->val = res[pos - 1].start >> 16 >> 16;
  190. bar->len_val = -resource_size(&res[pos - 1]) >> 16 >> 16;
  191. return bar;
  192. }
  193. }
  194. if (!res[pos].flags ||
  195. (res[pos].flags & (IORESOURCE_DISABLED | IORESOURCE_UNSET |
  196. IORESOURCE_BUSY)))
  197. return bar;
  198. bar->val = res[pos].start |
  199. (res[pos].flags & PCI_REGION_FLAG_MASK);
  200. bar->len_val = -resource_size(&res[pos]) |
  201. (res[pos].flags & PCI_REGION_FLAG_MASK);
  202. return bar;
  203. }
  204. static void bar_reset(struct pci_dev *dev, int offset, void *data)
  205. {
  206. struct pci_bar_info *bar = data;
  207. bar->which = 0;
  208. }
  209. static void bar_release(struct pci_dev *dev, int offset, void *data)
  210. {
  211. kfree(data);
  212. }
  213. static int xen_pcibk_read_vendor(struct pci_dev *dev, int offset,
  214. u16 *value, void *data)
  215. {
  216. *value = dev->vendor;
  217. return 0;
  218. }
  219. static int xen_pcibk_read_device(struct pci_dev *dev, int offset,
  220. u16 *value, void *data)
  221. {
  222. *value = dev->device;
  223. return 0;
  224. }
  225. static int interrupt_read(struct pci_dev *dev, int offset, u8 * value,
  226. void *data)
  227. {
  228. *value = (u8) dev->irq;
  229. return 0;
  230. }
  231. static int bist_write(struct pci_dev *dev, int offset, u8 value, void *data)
  232. {
  233. u8 cur_value;
  234. int err;
  235. err = pci_read_config_byte(dev, offset, &cur_value);
  236. if (err)
  237. goto out;
  238. if ((cur_value & ~PCI_BIST_START) == (value & ~PCI_BIST_START)
  239. || value == PCI_BIST_START)
  240. err = pci_write_config_byte(dev, offset, value);
  241. out:
  242. return err;
  243. }
  244. static const struct config_field header_common[] = {
  245. {
  246. .offset = PCI_VENDOR_ID,
  247. .size = 2,
  248. .u.w.read = xen_pcibk_read_vendor,
  249. },
  250. {
  251. .offset = PCI_DEVICE_ID,
  252. .size = 2,
  253. .u.w.read = xen_pcibk_read_device,
  254. },
  255. {
  256. .offset = PCI_COMMAND,
  257. .size = 2,
  258. .init = command_init,
  259. .release = bar_release,
  260. .u.w.read = command_read,
  261. .u.w.write = command_write,
  262. },
  263. {
  264. .offset = PCI_INTERRUPT_LINE,
  265. .size = 1,
  266. .u.b.read = interrupt_read,
  267. },
  268. {
  269. .offset = PCI_INTERRUPT_PIN,
  270. .size = 1,
  271. .u.b.read = xen_pcibk_read_config_byte,
  272. },
  273. {
  274. /* Any side effects of letting driver domain control cache line? */
  275. .offset = PCI_CACHE_LINE_SIZE,
  276. .size = 1,
  277. .u.b.read = xen_pcibk_read_config_byte,
  278. .u.b.write = xen_pcibk_write_config_byte,
  279. },
  280. {
  281. .offset = PCI_LATENCY_TIMER,
  282. .size = 1,
  283. .u.b.read = xen_pcibk_read_config_byte,
  284. },
  285. {
  286. .offset = PCI_BIST,
  287. .size = 1,
  288. .u.b.read = xen_pcibk_read_config_byte,
  289. .u.b.write = bist_write,
  290. },
  291. {}
  292. };
  293. #define CFG_FIELD_BAR(reg_offset) \
  294. { \
  295. .offset = reg_offset, \
  296. .size = 4, \
  297. .init = bar_init, \
  298. .reset = bar_reset, \
  299. .release = bar_release, \
  300. .u.dw.read = bar_read, \
  301. .u.dw.write = bar_write, \
  302. }
  303. #define CFG_FIELD_ROM(reg_offset) \
  304. { \
  305. .offset = reg_offset, \
  306. .size = 4, \
  307. .init = bar_init, \
  308. .reset = bar_reset, \
  309. .release = bar_release, \
  310. .u.dw.read = bar_read, \
  311. .u.dw.write = rom_write, \
  312. }
  313. static const struct config_field header_0[] = {
  314. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  315. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  316. CFG_FIELD_BAR(PCI_BASE_ADDRESS_2),
  317. CFG_FIELD_BAR(PCI_BASE_ADDRESS_3),
  318. CFG_FIELD_BAR(PCI_BASE_ADDRESS_4),
  319. CFG_FIELD_BAR(PCI_BASE_ADDRESS_5),
  320. CFG_FIELD_ROM(PCI_ROM_ADDRESS),
  321. {}
  322. };
  323. static const struct config_field header_1[] = {
  324. CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
  325. CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
  326. CFG_FIELD_ROM(PCI_ROM_ADDRESS1),
  327. {}
  328. };
  329. int xen_pcibk_config_header_add_fields(struct pci_dev *dev)
  330. {
  331. int err;
  332. err = xen_pcibk_config_add_fields(dev, header_common);
  333. if (err)
  334. goto out;
  335. switch (dev->hdr_type) {
  336. case PCI_HEADER_TYPE_NORMAL:
  337. err = xen_pcibk_config_add_fields(dev, header_0);
  338. break;
  339. case PCI_HEADER_TYPE_BRIDGE:
  340. err = xen_pcibk_config_add_fields(dev, header_1);
  341. break;
  342. default:
  343. err = -EINVAL;
  344. dev_err(&dev->dev, "Unsupported header type %d!\n",
  345. dev->hdr_type);
  346. break;
  347. }
  348. out:
  349. return err;
  350. }