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