Thomas Gleixner | c51669e | 2019-05-31 01:09:37 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2010 Matt Turner. |
| 4 | * Copyright 2012 Red Hat |
| 5 | * |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 6 | * Authors: Matthew Garrett |
| 7 | * Matt Turner |
| 8 | * Dave Airlie |
| 9 | */ |
Sam Ravnborg | 9f39780 | 2019-06-23 12:35:42 +0200 | [diff] [blame] | 10 | |
Thomas Zimmermann | 09daa2e | 2019-12-03 11:04:00 +0100 | [diff] [blame] | 11 | #include <linux/pci.h> |
| 12 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 13 | #include <drm/drm_crtc_helper.h> |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 14 | #include <drm/drm_gem_framebuffer_helper.h> |
Sam Ravnborg | 9f39780 | 2019-06-23 12:35:42 +0200 | [diff] [blame] | 15 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 16 | #include "mgag200_drv.h" |
| 17 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 18 | static const struct drm_mode_config_funcs mga_mode_funcs = { |
Thomas Zimmermann | 5d17718 | 2019-06-27 10:09:09 +0200 | [diff] [blame] | 19 | .fb_create = drm_gem_fb_create |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 20 | }; |
| 21 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 22 | static int mga_probe_vram(struct mga_device *mdev, void __iomem *mem) |
| 23 | { |
| 24 | int offset; |
| 25 | int orig; |
| 26 | int test1, test2; |
| 27 | int orig1, orig2; |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 28 | unsigned int vram_size; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 29 | |
| 30 | /* Probe */ |
| 31 | orig = ioread16(mem); |
| 32 | iowrite16(0, mem); |
| 33 | |
Mathieu Larouche | 6d857c1 | 2015-08-21 09:24:05 -0400 | [diff] [blame] | 34 | vram_size = mdev->mc.vram_window; |
| 35 | |
| 36 | if ((mdev->type == G200_EW3) && (vram_size >= 0x1000000)) { |
| 37 | vram_size = vram_size - 0x400000; |
| 38 | } |
| 39 | |
| 40 | for (offset = 0x100000; offset < vram_size; offset += 0x4000) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 41 | orig1 = ioread8(mem + offset); |
| 42 | orig2 = ioread8(mem + offset + 0x100); |
| 43 | |
| 44 | iowrite16(0xaa55, mem + offset); |
| 45 | iowrite16(0xaa55, mem + offset + 0x100); |
| 46 | |
| 47 | test1 = ioread16(mem + offset); |
| 48 | test2 = ioread16(mem); |
| 49 | |
| 50 | iowrite16(orig1, mem + offset); |
| 51 | iowrite16(orig2, mem + offset + 0x100); |
| 52 | |
| 53 | if (test1 != 0xaa55) { |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | if (test2) { |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | iowrite16(orig, mem); |
| 63 | return offset - 65536; |
| 64 | } |
| 65 | |
| 66 | /* Map the framebuffer from the card and configure the core */ |
| 67 | static int mga_vram_init(struct mga_device *mdev) |
| 68 | { |
| 69 | void __iomem *mem; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 70 | |
| 71 | /* BAR 0 is VRAM */ |
| 72 | mdev->mc.vram_base = pci_resource_start(mdev->dev->pdev, 0); |
| 73 | mdev->mc.vram_window = pci_resource_len(mdev->dev->pdev, 0); |
| 74 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 75 | if (!devm_request_mem_region(mdev->dev->dev, mdev->mc.vram_base, mdev->mc.vram_window, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 76 | "mgadrmfb_vram")) { |
| 77 | DRM_ERROR("can't reserve VRAM\n"); |
| 78 | return -ENXIO; |
| 79 | } |
| 80 | |
| 81 | mem = pci_iomap(mdev->dev->pdev, 0, 0); |
Arvind Yadav | 4b0ea93 | 2017-01-03 17:00:27 +0530 | [diff] [blame] | 82 | if (!mem) |
| 83 | return -ENOMEM; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 84 | |
| 85 | mdev->mc.vram_size = mga_probe_vram(mdev, mem); |
| 86 | |
| 87 | pci_iounmap(mdev->dev->pdev, mem); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int mgag200_device_init(struct drm_device *dev, |
| 93 | uint32_t flags) |
| 94 | { |
| 95 | struct mga_device *mdev = dev->dev_private; |
| 96 | int ret, option; |
| 97 | |
Thomas Zimmermann | d6d437d | 2019-11-26 11:15:28 +0100 | [diff] [blame] | 98 | mdev->flags = mgag200_flags_from_driver_data(flags); |
Thomas Zimmermann | 3a8a5aba | 2019-11-26 11:15:27 +0100 | [diff] [blame] | 99 | mdev->type = mgag200_type_from_driver_data(flags); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 100 | |
| 101 | /* Hardcode the number of CRTCs to 1 */ |
| 102 | mdev->num_crtc = 1; |
| 103 | |
| 104 | pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option); |
| 105 | mdev->has_sdram = !(option & (1 << 14)); |
| 106 | |
| 107 | /* BAR 0 is the framebuffer, BAR 1 contains registers */ |
| 108 | mdev->rmmio_base = pci_resource_start(mdev->dev->pdev, 1); |
| 109 | mdev->rmmio_size = pci_resource_len(mdev->dev->pdev, 1); |
| 110 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 111 | if (!devm_request_mem_region(mdev->dev->dev, mdev->rmmio_base, mdev->rmmio_size, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 112 | "mgadrmfb_mmio")) { |
| 113 | DRM_ERROR("can't reserve mmio registers\n"); |
| 114 | return -ENOMEM; |
| 115 | } |
| 116 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 117 | mdev->rmmio = pcim_iomap(dev->pdev, 1, 0); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 118 | if (mdev->rmmio == NULL) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | /* stash G200 SE model number for later use */ |
Thomas Zimmermann | c0826f4 | 2019-12-04 14:34:35 +0100 | [diff] [blame] | 122 | if (IS_G200_SE(mdev)) { |
Julia Lemire | abbee62 | 2013-06-27 13:38:59 -0400 | [diff] [blame] | 123 | mdev->unique_rev_id = RREG32(0x1e24); |
Thomas Zimmermann | c0826f4 | 2019-12-04 14:34:35 +0100 | [diff] [blame] | 124 | DRM_DEBUG("G200 SE unique revision id is 0x%x\n", |
| 125 | mdev->unique_rev_id); |
| 126 | } |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 127 | |
| 128 | ret = mga_vram_init(mdev); |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 129 | if (ret) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 130 | return ret; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 131 | |
| 132 | mdev->bpp_shifts[0] = 0; |
| 133 | mdev->bpp_shifts[1] = 1; |
| 134 | mdev->bpp_shifts[2] = 0; |
| 135 | mdev->bpp_shifts[3] = 2; |
| 136 | return 0; |
| 137 | } |
| 138 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Functions here will be called by the core once it's bound the driver to |
| 141 | * a PCI device |
| 142 | */ |
| 143 | |
| 144 | |
| 145 | int mgag200_driver_load(struct drm_device *dev, unsigned long flags) |
| 146 | { |
| 147 | struct mga_device *mdev; |
| 148 | int r; |
| 149 | |
Christopher Harvey | c2ed884 | 2013-04-05 16:15:30 +0000 | [diff] [blame] | 150 | mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 151 | if (mdev == NULL) |
| 152 | return -ENOMEM; |
| 153 | dev->dev_private = (void *)mdev; |
| 154 | mdev->dev = dev; |
| 155 | |
| 156 | r = mgag200_device_init(dev, flags); |
| 157 | if (r) { |
| 158 | dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r); |
Christopher Harvey | 2791197 | 2013-06-05 11:29:57 -0400 | [diff] [blame] | 159 | return r; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 160 | } |
| 161 | r = mgag200_mm_init(mdev); |
| 162 | if (r) |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 163 | goto err_mm; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 164 | |
| 165 | drm_mode_config_init(dev); |
| 166 | dev->mode_config.funcs = (void *)&mga_mode_funcs; |
Thomas Zimmermann | 2c51a66 | 2019-09-27 11:13:00 +0200 | [diff] [blame] | 167 | if (IS_G200_SE(mdev) && mdev->vram_fb_available < (2048*1024)) |
Dave Airlie | 918be88 | 2014-01-21 01:47:46 -0500 | [diff] [blame] | 168 | dev->mode_config.preferred_depth = 16; |
| 169 | else |
Thomas Zimmermann | 90f479a | 2019-07-03 09:58:42 +0200 | [diff] [blame] | 170 | dev->mode_config.preferred_depth = 32; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 171 | dev->mode_config.prefer_shadow = 1; |
| 172 | |
| 173 | r = mgag200_modeset_init(mdev); |
Christopher Harvey | a080db9 | 2013-06-05 15:24:26 -0400 | [diff] [blame] | 174 | if (r) { |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 175 | dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r); |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 176 | goto err_modeset; |
Christopher Harvey | a080db9 | 2013-06-05 15:24:26 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Thomas Zimmermann | 49b8d5a | 2019-09-27 11:12:56 +0200 | [diff] [blame] | 179 | r = mgag200_cursor_init(mdev); |
| 180 | if (r) |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 181 | dev_warn(&dev->pdev->dev, |
Thomas Zimmermann | 49b8d5a | 2019-09-27 11:12:56 +0200 | [diff] [blame] | 182 | "Could not initialize cursors. Not doing hardware cursors.\n"); |
Christopher Harvey | a080db9 | 2013-06-05 15:24:26 -0400 | [diff] [blame] | 183 | |
Thomas Zimmermann | 90f479a | 2019-07-03 09:58:42 +0200 | [diff] [blame] | 184 | r = drm_fbdev_generic_setup(mdev->dev, 0); |
| 185 | if (r) |
| 186 | goto err_modeset; |
| 187 | |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 188 | return 0; |
| 189 | |
| 190 | err_modeset: |
| 191 | drm_mode_config_cleanup(dev); |
Thomas Zimmermann | 49b8d5a | 2019-09-27 11:12:56 +0200 | [diff] [blame] | 192 | mgag200_cursor_fini(mdev); |
Archit Taneja | 728f866 | 2015-09-17 16:30:55 +0530 | [diff] [blame] | 193 | mgag200_mm_fini(mdev); |
| 194 | err_mm: |
| 195 | dev->dev_private = NULL; |
| 196 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 197 | return r; |
| 198 | } |
| 199 | |
Gabriel Krisman Bertazi | 11b3c20 | 2017-01-06 15:57:31 -0200 | [diff] [blame] | 200 | void mgag200_driver_unload(struct drm_device *dev) |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 201 | { |
| 202 | struct mga_device *mdev = dev->dev_private; |
| 203 | |
| 204 | if (mdev == NULL) |
Gabriel Krisman Bertazi | 11b3c20 | 2017-01-06 15:57:31 -0200 | [diff] [blame] | 205 | return; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 206 | mgag200_modeset_fini(mdev); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 207 | drm_mode_config_cleanup(dev); |
Thomas Zimmermann | 49b8d5a | 2019-09-27 11:12:56 +0200 | [diff] [blame] | 208 | mgag200_cursor_fini(mdev); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 209 | mgag200_mm_fini(mdev); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 210 | dev->dev_private = NULL; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 211 | } |