conf_space_header.c 9.8 KB

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