mgag200_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright 2010 Matt Turner.
  3. * Copyright 2012 Red Hat
  4. *
  5. * This file is subject to the terms and conditions of the GNU General
  6. * Public License version 2. See the file COPYING in the main
  7. * directory of this archive for more details.
  8. *
  9. * Authors: Matthew Garrett
  10. * Matt Turner
  11. * Dave Airlie
  12. */
  13. #include <drm/drmP.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include "mgag200_drv.h"
  16. static void mga_user_framebuffer_destroy(struct drm_framebuffer *fb)
  17. {
  18. struct mga_framebuffer *mga_fb = to_mga_framebuffer(fb);
  19. if (mga_fb->obj)
  20. drm_gem_object_unreference_unlocked(mga_fb->obj);
  21. drm_framebuffer_cleanup(fb);
  22. kfree(fb);
  23. }
  24. static const struct drm_framebuffer_funcs mga_fb_funcs = {
  25. .destroy = mga_user_framebuffer_destroy,
  26. };
  27. int mgag200_framebuffer_init(struct drm_device *dev,
  28. struct mga_framebuffer *gfb,
  29. struct drm_mode_fb_cmd2 *mode_cmd,
  30. struct drm_gem_object *obj)
  31. {
  32. int ret;
  33. drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd);
  34. gfb->obj = obj;
  35. ret = drm_framebuffer_init(dev, &gfb->base, &mga_fb_funcs);
  36. if (ret) {
  37. DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
  38. return ret;
  39. }
  40. return 0;
  41. }
  42. static struct drm_framebuffer *
  43. mgag200_user_framebuffer_create(struct drm_device *dev,
  44. struct drm_file *filp,
  45. struct drm_mode_fb_cmd2 *mode_cmd)
  46. {
  47. struct drm_gem_object *obj;
  48. struct mga_framebuffer *mga_fb;
  49. int ret;
  50. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  51. if (obj == NULL)
  52. return ERR_PTR(-ENOENT);
  53. mga_fb = kzalloc(sizeof(*mga_fb), GFP_KERNEL);
  54. if (!mga_fb) {
  55. drm_gem_object_unreference_unlocked(obj);
  56. return ERR_PTR(-ENOMEM);
  57. }
  58. ret = mgag200_framebuffer_init(dev, mga_fb, mode_cmd, obj);
  59. if (ret) {
  60. drm_gem_object_unreference_unlocked(obj);
  61. kfree(mga_fb);
  62. return ERR_PTR(ret);
  63. }
  64. return &mga_fb->base;
  65. }
  66. static const struct drm_mode_config_funcs mga_mode_funcs = {
  67. .fb_create = mgag200_user_framebuffer_create,
  68. };
  69. static int mga_probe_vram(struct mga_device *mdev, void __iomem *mem)
  70. {
  71. int offset;
  72. int orig;
  73. int test1, test2;
  74. int orig1, orig2;
  75. unsigned int vram_size;
  76. /* Probe */
  77. orig = ioread16(mem);
  78. iowrite16(0, mem);
  79. vram_size = mdev->mc.vram_window;
  80. if ((mdev->type == G200_EW3) && (vram_size >= 0x1000000)) {
  81. vram_size = vram_size - 0x400000;
  82. }
  83. for (offset = 0x100000; offset < vram_size; offset += 0x4000) {
  84. orig1 = ioread8(mem + offset);
  85. orig2 = ioread8(mem + offset + 0x100);
  86. iowrite16(0xaa55, mem + offset);
  87. iowrite16(0xaa55, mem + offset + 0x100);
  88. test1 = ioread16(mem + offset);
  89. test2 = ioread16(mem);
  90. iowrite16(orig1, mem + offset);
  91. iowrite16(orig2, mem + offset + 0x100);
  92. if (test1 != 0xaa55) {
  93. break;
  94. }
  95. if (test2) {
  96. break;
  97. }
  98. }
  99. iowrite16(orig, mem);
  100. return offset - 65536;
  101. }
  102. /* Map the framebuffer from the card and configure the core */
  103. static int mga_vram_init(struct mga_device *mdev)
  104. {
  105. void __iomem *mem;
  106. struct apertures_struct *aper = alloc_apertures(1);
  107. if (!aper)
  108. return -ENOMEM;
  109. /* BAR 0 is VRAM */
  110. mdev->mc.vram_base = pci_resource_start(mdev->dev->pdev, 0);
  111. mdev->mc.vram_window = pci_resource_len(mdev->dev->pdev, 0);
  112. aper->ranges[0].base = mdev->mc.vram_base;
  113. aper->ranges[0].size = mdev->mc.vram_window;
  114. remove_conflicting_framebuffers(aper, "mgafb", true);
  115. kfree(aper);
  116. if (!devm_request_mem_region(mdev->dev->dev, mdev->mc.vram_base, mdev->mc.vram_window,
  117. "mgadrmfb_vram")) {
  118. DRM_ERROR("can't reserve VRAM\n");
  119. return -ENXIO;
  120. }
  121. mem = pci_iomap(mdev->dev->pdev, 0, 0);
  122. if (!mem)
  123. return -ENOMEM;
  124. mdev->mc.vram_size = mga_probe_vram(mdev, mem);
  125. pci_iounmap(mdev->dev->pdev, mem);
  126. return 0;
  127. }
  128. static int mgag200_device_init(struct drm_device *dev,
  129. uint32_t flags)
  130. {
  131. struct mga_device *mdev = dev->dev_private;
  132. int ret, option;
  133. mdev->type = flags;
  134. /* Hardcode the number of CRTCs to 1 */
  135. mdev->num_crtc = 1;
  136. pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
  137. mdev->has_sdram = !(option & (1 << 14));
  138. /* BAR 0 is the framebuffer, BAR 1 contains registers */
  139. mdev->rmmio_base = pci_resource_start(mdev->dev->pdev, 1);
  140. mdev->rmmio_size = pci_resource_len(mdev->dev->pdev, 1);
  141. if (!devm_request_mem_region(mdev->dev->dev, mdev->rmmio_base, mdev->rmmio_size,
  142. "mgadrmfb_mmio")) {
  143. DRM_ERROR("can't reserve mmio registers\n");
  144. return -ENOMEM;
  145. }
  146. mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
  147. if (mdev->rmmio == NULL)
  148. return -ENOMEM;
  149. /* stash G200 SE model number for later use */
  150. if (IS_G200_SE(mdev))
  151. mdev->unique_rev_id = RREG32(0x1e24);
  152. ret = mga_vram_init(mdev);
  153. if (ret)
  154. return ret;
  155. mdev->bpp_shifts[0] = 0;
  156. mdev->bpp_shifts[1] = 1;
  157. mdev->bpp_shifts[2] = 0;
  158. mdev->bpp_shifts[3] = 2;
  159. return 0;
  160. }
  161. /*
  162. * Functions here will be called by the core once it's bound the driver to
  163. * a PCI device
  164. */
  165. int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
  166. {
  167. struct mga_device *mdev;
  168. int r;
  169. mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL);
  170. if (mdev == NULL)
  171. return -ENOMEM;
  172. dev->dev_private = (void *)mdev;
  173. mdev->dev = dev;
  174. r = mgag200_device_init(dev, flags);
  175. if (r) {
  176. dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
  177. return r;
  178. }
  179. r = mgag200_mm_init(mdev);
  180. if (r)
  181. goto err_mm;
  182. drm_mode_config_init(dev);
  183. dev->mode_config.funcs = (void *)&mga_mode_funcs;
  184. if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
  185. dev->mode_config.preferred_depth = 16;
  186. else
  187. dev->mode_config.preferred_depth = 24;
  188. dev->mode_config.prefer_shadow = 1;
  189. r = mgag200_modeset_init(mdev);
  190. if (r) {
  191. dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
  192. goto err_modeset;
  193. }
  194. /* Make small buffers to store a hardware cursor (double buffered icon updates) */
  195. mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0,
  196. &mdev->cursor.pixels_1);
  197. mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0,
  198. &mdev->cursor.pixels_2);
  199. if (!mdev->cursor.pixels_2 || !mdev->cursor.pixels_1) {
  200. mdev->cursor.pixels_1 = NULL;
  201. mdev->cursor.pixels_2 = NULL;
  202. dev_warn(&dev->pdev->dev,
  203. "Could not allocate space for cursors. Not doing hardware cursors.\n");
  204. } else {
  205. mdev->cursor.pixels_current = mdev->cursor.pixels_1;
  206. mdev->cursor.pixels_prev = mdev->cursor.pixels_2;
  207. }
  208. return 0;
  209. err_modeset:
  210. drm_mode_config_cleanup(dev);
  211. mgag200_mm_fini(mdev);
  212. err_mm:
  213. dev->dev_private = NULL;
  214. return r;
  215. }
  216. int mgag200_driver_unload(struct drm_device *dev)
  217. {
  218. struct mga_device *mdev = dev->dev_private;
  219. if (mdev == NULL)
  220. return 0;
  221. mgag200_modeset_fini(mdev);
  222. mgag200_fbdev_fini(mdev);
  223. drm_mode_config_cleanup(dev);
  224. mgag200_mm_fini(mdev);
  225. dev->dev_private = NULL;
  226. return 0;
  227. }
  228. int mgag200_gem_create(struct drm_device *dev,
  229. u32 size, bool iskernel,
  230. struct drm_gem_object **obj)
  231. {
  232. struct mgag200_bo *astbo;
  233. int ret;
  234. *obj = NULL;
  235. size = roundup(size, PAGE_SIZE);
  236. if (size == 0)
  237. return -EINVAL;
  238. ret = mgag200_bo_create(dev, size, 0, 0, &astbo);
  239. if (ret) {
  240. if (ret != -ERESTARTSYS)
  241. DRM_ERROR("failed to allocate GEM object\n");
  242. return ret;
  243. }
  244. *obj = &astbo->gem;
  245. return 0;
  246. }
  247. int mgag200_dumb_create(struct drm_file *file,
  248. struct drm_device *dev,
  249. struct drm_mode_create_dumb *args)
  250. {
  251. int ret;
  252. struct drm_gem_object *gobj;
  253. u32 handle;
  254. args->pitch = args->width * ((args->bpp + 7) / 8);
  255. args->size = args->pitch * args->height;
  256. ret = mgag200_gem_create(dev, args->size, false,
  257. &gobj);
  258. if (ret)
  259. return ret;
  260. ret = drm_gem_handle_create(file, gobj, &handle);
  261. drm_gem_object_unreference_unlocked(gobj);
  262. if (ret)
  263. return ret;
  264. args->handle = handle;
  265. return 0;
  266. }
  267. static void mgag200_bo_unref(struct mgag200_bo **bo)
  268. {
  269. struct ttm_buffer_object *tbo;
  270. if ((*bo) == NULL)
  271. return;
  272. tbo = &((*bo)->bo);
  273. ttm_bo_unref(&tbo);
  274. *bo = NULL;
  275. }
  276. void mgag200_gem_free_object(struct drm_gem_object *obj)
  277. {
  278. struct mgag200_bo *mgag200_bo = gem_to_mga_bo(obj);
  279. mgag200_bo_unref(&mgag200_bo);
  280. }
  281. static inline u64 mgag200_bo_mmap_offset(struct mgag200_bo *bo)
  282. {
  283. return drm_vma_node_offset_addr(&bo->bo.vma_node);
  284. }
  285. int
  286. mgag200_dumb_mmap_offset(struct drm_file *file,
  287. struct drm_device *dev,
  288. uint32_t handle,
  289. uint64_t *offset)
  290. {
  291. struct drm_gem_object *obj;
  292. struct mgag200_bo *bo;
  293. obj = drm_gem_object_lookup(dev, file, handle);
  294. if (obj == NULL)
  295. return -ENOENT;
  296. bo = gem_to_mga_bo(obj);
  297. *offset = mgag200_bo_mmap_offset(bo);
  298. drm_gem_object_unreference_unlocked(obj);
  299. return 0;
  300. }