conf_space_header.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * PCI Backend - Handles the virtual fields in the configuration space headers.
  3. *
  4. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/pci.h>
  9. #include "pciback.h"
  10. #include "conf_space.h"
  11. struct pci_cmd_info {
  12. u16 val;
  13. };
  14. struct pci_bar_info {
  15. u32 val;
  16. u32 len_val;
  17. int which;
  18. };
  19. #define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO))
  20. #define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER)
  21. /* Bits guests are allowed to control in permissive mode. */
  22. #define PCI_COMMAND_GUEST (PCI_COMMAND_MASTER|PCI_COMMAND_SPECIAL| \
  23. PCI_COMMAND_INVALIDATE|PCI_COMMAND_VGA_PALETTE| \
  24. PCI_COMMAND_WAIT|PCI_COMMAND_FAST_BACK)
  25. static void *command_init(struct pci_dev *dev, int offset)
  26. {
  27. struct pci_cmd_info *cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
  28. int err;
  29. if (!cmd)
  30. return ERR_PTR(-ENOMEM);
  31. err = pci_read_config_word(dev, PCI_COMMAND, &cmd->val);
  32. if (err) {
  33. kfree(cmd);
  34. return ERR_PTR(err);
  35. }
  36. return cmd;
  37. }
  38. static int command_read(struct pci_dev *dev, int offset, u16 *value, void *data)
  39. {
  40. int ret = pci_read_config_word(dev, offset, value);
  41. const struct pci_cmd_info *cmd = data;
  42. *value &= PCI_COMMAND_GUEST;
  43. *value |= cmd->val & ~PCI_COMMAND_GUEST;
  44. return ret;
  45. }
  46. static int command_write(struct pci_dev *dev, int offset, u16 value, void *data)
  47. {
  48. struct xen_pcibk_dev_data *dev_data;
  49. int err;
  50. u16 val;
  51. struct pci_cmd_info *cmd = data;
  52. dev_data = pci_get_drvdata(dev);
  53. if (!pci_is_enabled(dev) && is_enable_cmd(value)) {
  54. if (unlikely(verbose_request))
  55. printk(KERN_DEBUG DRV_NAME ": %s: enable\n",
  56. pci_name(dev));
  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. if (unlikely(verbose_request))
  64. printk(KERN_DEBUG DRV_NAME ": %s: disable\n",
  65. pci_name(dev));
  66. pci_disable_device(dev);
  67. if (dev_data)
  68. dev_data->enable_intx = 0;
  69. }
  70. if (!dev->is_busmaster && is_master_cmd(value)) {
  71. if (unlikely(verbose_request))
  72. printk(KERN_DEBUG DRV_NAME ": %s: set bus master\n",
  73. pci_name(dev));
  74. pci_set_master(dev);
  75. }
  76. if (value & PCI_COMMAND_INVALIDATE) {
  77. if (unlikely(verbose_request))
  78. printk(KERN_DEBUG
  79. DRV_NAME ": %s: enable memory-write-invalidate\n",
  80. pci_name(dev));
  81. err = pci_set_mwi(dev);
  82. if (err) {
  83. pr_warn("%s: cannot enable memory-write-invalidate (%d)\n",
  84. pci_name(dev), err);
  85. value &= ~PCI_COMMAND_INVALIDATE;
  86. }
  87. }
  88. cmd->val = value;
  89. if (!xen_pcibk_permissive && (!dev_data || !dev_data->permissive))
  90. return 0;
  91. /* Only allow the guest to control certain bits. */
  92. err = pci_read_config_word(dev, offset, &val);
  93. if (err || val == value)
  94. return err;
  95. value &= PCI_COMMAND_GUEST;
  96. value |= val & ~PCI_COMMAND_GUEST;
  97. return pci_write_config_word(dev, offset, value);
  98. }
  99. static int rom_write(struct pci_dev *dev, int offset, u32 value, void *data)
  100. {
  101. struct pci_bar_info *bar = data;
  102. if (unlikely(!bar)) {
  103. pr_warn(DRV_NAME ": driver data not found for %s\n",
  104. pci_name(dev));
  105. return XEN_PCI_ERR_op_failed;
  106. }
  107. /* A write to obtain the length must happen as a 32-bit write.
  108. * This does not (yet) support writing individual bytes
  109. */
  110. if (value == ~PCI_ROM_ADDRESS_ENABLE)
  111. bar->which = 1;
  112. else {
  113. u32 tmpval;
  114. pci_read_config_dword(dev, offset, &tmpval);
  115. if (tmpval != bar->val && value == bar->val) {
  116. /* Allow restoration of bar value. */
  117. pci_write_config_dword(dev, offset, bar->val);
  118. }
  119. bar->which = 0;
  120. }
  121. /* Do we need to support enabling/disabling the rom address here? */
  122. return 0;
  123. }
  124. /* For the BARs, only allow writes which write ~0 or
  125. * the correct resource information
  126. * (Needed for when the driver probes the resource usage)
  127. */
  128. static int bar_write(struct pci_dev *dev, int offset, u32 value, void *data)
  129. {
  130. struct pci_bar_info *bar = data;
  131. if (unlikely(!bar)) {
  132. pr_warn(DRV_NAME ": driver data not found for %s\n",
  133. pci_name(dev));
  134. return XEN_PCI_ERR_op_failed;
  135. }
  136. /* A write to obtain the length must happen as a 32-bit write.
  137. * This does not (yet) support writing individual bytes
  138. */
  139. if (value == ~0)
  140. bar->which = 1;
  141. else {
  142. u32 tmpval;
  143. pci_read_config_dword(dev, offset, &tmpval);
  144. if (tmpval != bar->val && value == bar->val) {
  145. /* Allow restoration of bar value. */
  146. pci_write_config_dword(dev, offset, bar->val);
  147. }
  148. bar->which = 0;
  149. }
  150. return 0;
  151. }
  152. static int bar_read(struct pci_dev *dev, int offset, u32 * value, void *data)
  153. {
  154. struct pci_bar_info *bar = data;
  155. if (unlikely(!bar)) {
  156. pr_warn(DRV_NAME ": driver data not found for %s\n",
  157. pci_name(dev));
  158. return XEN_PCI_ERR_op_failed;
  159. }
  160. *value = bar->which ? bar->len_val : bar->val;
  161. return 0;
  162. }
  163. static inline void read_dev_bar(struct pci_dev *dev,
  164. struct pci_bar_info *bar_info, int offset,
  165. u32 len_mask)
  166. {
  167. int pos;
  168. struct resource *res = dev->resource;
  169. if (offset == PCI_ROM_ADDRESS || offset == PCI_ROM_ADDRESS1)
  170. pos = PCI_ROM_RESOURCE;
  171. else {
  172. pos = (offset - PCI_BASE_ADDRESS_0) / 4;
  173. if (pos && ((res[pos - 1].flags & (PCI_BASE_ADDRESS_SPACE |
  174. PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  175. (PCI_BASE_ADDRESS_SPACE_MEMORY |
  176. PCI_BASE_ADDRESS_MEM_TYPE_64))) {
  177. bar_info->val = res[pos - 1].start >> 32;
  178. bar_info->len_val = res[pos - 1].end >> 32;
  179. return;
  180. }
  181. }
  182. bar_info->val = res[pos].start |
  183. (res[pos].flags & PCI_REGION_FLAG_MASK);
  184. bar_info->len_val = resource_size(&res[pos]);
  185. }
  186. static void *bar_init(struct pci_dev *dev, int offset)
  187. {
  188. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  189. if (!bar)
  190. return ERR_PTR(-ENOMEM);
  191. read_dev_bar(dev, bar, offset, ~0);
  192. bar->which = 0;
  193. return bar;
  194. }
  195. static void *rom_init(struct pci_dev *dev, int offset)
  196. {
  197. struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);
  198. if (!bar)
  199. return ERR_PTR(-ENOMEM);
  200. read_dev_bar(dev, bar, offset, ~PCI_ROM_ADDRESS_ENABLE);
  201. bar->which = 0;
  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 = rom_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. pr_err("%s: Unsupported header type %d!\n",
  345. pci_name(dev), dev->hdr_type);
  346. break;
  347. }
  348. out:
  349. return err;
  350. }