Paulo Zanoni | 3b65f17 | 2012-02-15 16:04:28 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Paulo Zanoni <paulo.r.zanoni@intel.com> |
| 25 | */ |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <stdio.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
Daniel Vetter | c03c6ce | 2014-03-22 21:34:29 +0100 | [diff] [blame] | 33 | #include "intel_io.h" |
Daniel Vetter | 6cfcd71 | 2014-03-22 20:07:35 +0100 | [diff] [blame] | 34 | #include "intel_chipset.h" |
| 35 | #include "intel_reg.h" |
Paulo Zanoni | 3b65f17 | 2012-02-15 16:04:28 -0200 | [diff] [blame] | 36 | |
| 37 | int gen; |
| 38 | |
| 39 | uint32_t HTOTAL[] = { 0x60000, 0x61000, 0x62000 }; |
| 40 | uint32_t VTOTAL[] = { 0x6000C, 0x6100C, 0x6200C }; |
| 41 | uint32_t PIPECONF[] = { 0x70008, 0x71008, 0x72008 }; |
| 42 | uint32_t PIPESRC[] = { 0x6001C, 0x6101C, 0x6201C }; |
| 43 | uint32_t PF_CTRL1[] = { 0x68080, 0x68880, 0x69080 }; |
| 44 | uint32_t PF_WIN_POS[] = { 0x68070, 0x68870, 0x69070 }; |
| 45 | uint32_t PF_WIN_SZ[] = { 0x68074, 0x68874, 0x69074 }; |
| 46 | |
| 47 | #define PIPECONF_ENABLE (1 << 31) |
| 48 | #define PIPECONF_INTERLACE_MASK (7 << 21) |
| 49 | #define PIPECONF_PF_PD (0 << 21) |
| 50 | #define PIPECONF_PF_ID (1 << 21) |
| 51 | #define PIPECONF_IF_ID (3 << 21) |
| 52 | |
| 53 | #define HTOTAL_ACTIVE_MASK (0xFFF << 0) |
| 54 | #define VTOTAL_ACTIVE_MASK (0xFFF << 0) |
| 55 | |
| 56 | #define PIPESRC_HORIZ_MASK (0xFFF << 16) |
| 57 | #define PIPESRC_VERT_MASK (0xFFF << 0) |
| 58 | |
| 59 | /*#define PF_ENABLE (1 << 31)*/ |
| 60 | #define PF_PIPE_MASK (3 << 29) |
| 61 | #define PF_FILTER_MASK (3 << 23) |
| 62 | #define PF_FILTER_MED (1 << 23) |
| 63 | #define PF_PIPE_A (0 << 29) |
| 64 | #define PF_PIPE_B (1 << 29) |
| 65 | #define PF_PIPE_C (2 << 29) |
| 66 | |
| 67 | #define PF_WIN_SZ_X_MASK (0x1FFF << 16) |
| 68 | #define PF_WIN_SZ_Y_MASK (0xFFF << 0) |
| 69 | |
| 70 | struct pipe_info { |
| 71 | bool enabled; |
| 72 | bool pf_enabled; |
| 73 | uint32_t interlace_mode; |
| 74 | uint32_t tot_width; /* htotal */ |
| 75 | uint32_t tot_height; /* vtotal */ |
| 76 | uint32_t src_width; /* pipesrc.x */ |
| 77 | uint32_t src_height; /* pipesrc.y */ |
| 78 | uint32_t dst_width; /* pf_win_sz.x */ |
| 79 | uint32_t dst_height; /* pf_win_sz.y */ |
| 80 | }; |
| 81 | |
| 82 | static void read_pipe_info(int intel_pipe, struct pipe_info *info) |
| 83 | { |
| 84 | uint32_t conf, vtotal, htotal, src, ctrl1, win_sz; |
| 85 | |
| 86 | conf = INREG(PIPECONF[intel_pipe]); |
| 87 | htotal = INREG(HTOTAL[intel_pipe]); |
| 88 | vtotal = INREG(VTOTAL[intel_pipe]); |
| 89 | src = INREG(PIPESRC[intel_pipe]); |
| 90 | ctrl1 = INREG(PF_CTRL1[intel_pipe]); |
| 91 | win_sz = INREG(PF_WIN_SZ[intel_pipe]); |
| 92 | |
| 93 | info->enabled = (conf & PIPECONF_ENABLE) ? true : false; |
| 94 | info->tot_width = (htotal & HTOTAL_ACTIVE_MASK) + 1; |
| 95 | info->tot_height = (vtotal & VTOTAL_ACTIVE_MASK) + 1; |
| 96 | info->src_width = ((src & PIPESRC_HORIZ_MASK) >> 16) + 1; |
| 97 | info->src_height = (src & PIPESRC_VERT_MASK) + 1; |
| 98 | info->interlace_mode = conf & PIPECONF_INTERLACE_MASK; |
| 99 | info->pf_enabled = ctrl1 & PF_ENABLE; |
| 100 | info->dst_width = (win_sz & PF_WIN_SZ_X_MASK) >> 16; |
| 101 | info->dst_height = win_sz & PF_WIN_SZ_Y_MASK; |
| 102 | } |
| 103 | |
| 104 | static void dump_pipe(int intel_pipe) |
| 105 | { |
| 106 | struct pipe_info info; |
| 107 | |
| 108 | read_pipe_info(intel_pipe, &info); |
| 109 | |
| 110 | printf("\nPipe %c:\n", intel_pipe + 'A'); |
| 111 | |
| 112 | printf("- %s\n", info.enabled ? "enabled" : "disabled"); |
| 113 | if (!info.enabled) |
| 114 | return; |
| 115 | |
| 116 | switch (info.interlace_mode) { |
| 117 | case PIPECONF_PF_PD: |
| 118 | printf("- progressive\n"); |
| 119 | break; |
| 120 | case PIPECONF_PF_ID: |
| 121 | printf("- interlaced (progressive fetch)\n"); |
| 122 | break; |
| 123 | case PIPECONF_IF_ID: |
| 124 | printf("- interlaced (interlaced fetch)\n"); |
| 125 | break; |
| 126 | default: |
| 127 | assert(0); |
| 128 | } |
| 129 | |
| 130 | printf("- pf %s\n", info.pf_enabled ? "enabled" : "disabled"); |
| 131 | if (!info.pf_enabled) |
| 132 | return; |
| 133 | |
| 134 | printf("- tot %dx%d\n", info.tot_width, info.tot_height); |
| 135 | printf("- src %dx%d\n", info.src_width, info.src_height); |
| 136 | printf("- dst %dx%d\n", info.dst_width, info.dst_height); |
| 137 | } |
| 138 | |
| 139 | static void dump_info(void) |
| 140 | { |
| 141 | int i; |
| 142 | int pipes; |
| 143 | |
| 144 | if (gen < 7) |
| 145 | pipes = 2; |
| 146 | else |
| 147 | pipes = 3; |
| 148 | |
| 149 | for (i = 0; i < pipes; i++) { |
| 150 | dump_pipe(i); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static int change_screen_size(int intel_pipe, int x, int y) |
| 155 | { |
| 156 | struct pipe_info info; |
| 157 | uint32_t dst_width, dst_height, pos_x, pos_y; |
| 158 | uint32_t ctrl1_val; |
| 159 | uint32_t win_pos_val; |
| 160 | uint32_t win_sz_val; |
| 161 | |
| 162 | read_pipe_info(intel_pipe, &info); |
| 163 | |
| 164 | if (x == 0) { |
| 165 | if (info.dst_width != 0) |
| 166 | dst_width = info.dst_width; |
| 167 | else |
| 168 | dst_width = info.src_width; |
| 169 | } else { |
| 170 | dst_width = x; |
| 171 | } |
| 172 | |
| 173 | if (y == 0) { |
| 174 | if (info.dst_height != 0) |
| 175 | dst_height = info.dst_height; |
| 176 | else |
| 177 | dst_height = info.src_height; |
| 178 | } else { |
| 179 | dst_height = y; |
| 180 | } |
| 181 | |
| 182 | pos_x = abs((info.tot_width - dst_width)) / 2; |
| 183 | pos_y = abs((info.tot_height - dst_height)) / 2; |
| 184 | |
| 185 | if (pos_x == 1) |
| 186 | pos_x = 0; |
| 187 | |
| 188 | if (info.src_width / (double) dst_width > 1.125) { |
| 189 | printf("X is too small\n"); |
| 190 | return 1; |
| 191 | } else if (info.tot_width < dst_width) { |
| 192 | printf("X is too big\n"); |
| 193 | return 1; |
| 194 | } else if (dst_width & 1) { |
| 195 | printf("X must be even\n"); |
| 196 | return 1; |
| 197 | } else if (info.src_height / (double) dst_height > 1.125) { |
| 198 | printf("Y is too small\n"); |
| 199 | return 1; |
| 200 | } else if (info.tot_height < dst_height) { |
| 201 | printf("Y is too big\n"); |
| 202 | return 1; |
| 203 | } else if (dst_height & 1) { |
| 204 | printf("Y must be even\n"); |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | printf("Changing size for pipe %c:\n" |
| 209 | "- width: %d -> %d\n" |
| 210 | "- height: %d -> %d\n" |
| 211 | "- pos: %dx%d\n", |
| 212 | intel_pipe + 'A', info.src_width, dst_width, info.src_height, |
| 213 | dst_height, pos_x, pos_y); |
| 214 | |
| 215 | ctrl1_val = PF_ENABLE | PF_FILTER_MED; |
| 216 | |
| 217 | /* This can break stuff if the panel fitter is already enabled for |
| 218 | * another pipe */ |
| 219 | if (gen >= 7) { |
| 220 | switch (intel_pipe) { |
| 221 | case 0: |
| 222 | ctrl1_val |= PF_PIPE_A; |
| 223 | break; |
| 224 | case 1: |
| 225 | ctrl1_val |= PF_PIPE_B; |
| 226 | break; |
| 227 | case 2: |
| 228 | ctrl1_val |= PF_PIPE_C; |
| 229 | break; |
| 230 | default: |
| 231 | assert(0); |
| 232 | } |
| 233 | } |
| 234 | OUTREG(PF_CTRL1[intel_pipe], ctrl1_val); |
| 235 | |
| 236 | win_pos_val = pos_x << 16; |
| 237 | win_pos_val |= pos_y; |
| 238 | OUTREG(PF_WIN_POS[intel_pipe], win_pos_val); |
| 239 | |
| 240 | win_sz_val = dst_width << 16; |
| 241 | win_sz_val |= dst_height; |
| 242 | OUTREG(PF_WIN_SZ[intel_pipe], win_sz_val); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int disable_panel_fitter(int intel_pipe) |
| 248 | { |
| 249 | OUTREG(PF_CTRL1[intel_pipe], 0); |
| 250 | OUTREG(PF_WIN_POS[intel_pipe], 0); |
| 251 | OUTREG(PF_WIN_SZ[intel_pipe], 0); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static void print_usage(void) |
| 256 | { |
| 257 | printf("Options:\n" |
| 258 | " -p pipe: pipe to be used (A, B or C)\n" |
| 259 | " -x value: final screen width size in pixels\n" |
| 260 | " -y value: final screen height size in pixels\n" |
| 261 | " -d: disable panel fitter\n" |
| 262 | " -l: list the current state of each pipe\n" |
| 263 | " -h: prints this message\n"); |
| 264 | } |
| 265 | |
| 266 | int main (int argc, char *argv[]) |
| 267 | { |
| 268 | int opt; |
| 269 | int ret = 0; |
| 270 | char intel_pipe = '\0'; |
| 271 | int x = 0, y = 0; |
| 272 | bool do_disable = false, do_dump = false, do_usage = false; |
| 273 | struct pci_device *pci_dev; |
| 274 | uint32_t devid; |
| 275 | |
| 276 | printf("WARNING:\n" |
| 277 | "This tool is a workaround for people that don't have a Kernel " |
| 278 | "with overscan compensation properties: it is just a temporary " |
| 279 | "solution that may or may not work. Use it at your own risk.\n"); |
| 280 | |
| 281 | pci_dev = intel_get_pci_device(); |
| 282 | intel_register_access_init(pci_dev, 0); |
| 283 | devid = pci_dev->device_id; |
| 284 | |
| 285 | if (!HAS_PCH_SPLIT(devid)) { |
| 286 | printf("This tool was only tested on Ironlake and newer\n"); |
| 287 | ret = 1; |
| 288 | goto out; |
| 289 | } |
| 290 | if (IS_GEN5(devid)) |
| 291 | gen = 5; |
| 292 | else if (IS_GEN6(devid)) |
| 293 | gen = 6; |
| 294 | else |
| 295 | gen = 7; |
| 296 | |
| 297 | while ((opt = getopt(argc, argv, "p:x:y:dlh")) != -1) { |
| 298 | switch (opt) { |
| 299 | case 'p': |
| 300 | intel_pipe = optarg[0]; |
| 301 | if (intel_pipe != 'A' && intel_pipe != 'B' && |
| 302 | (gen <= 6 || intel_pipe != 'C')) { |
| 303 | printf("Invalid pipe\n"); |
| 304 | ret = 1; |
| 305 | goto out; |
| 306 | } |
| 307 | break; |
| 308 | case 'x': |
| 309 | x = atoi(optarg); |
| 310 | break; |
| 311 | case 'y': |
| 312 | y = atoi(optarg); |
| 313 | break; |
| 314 | case 'd': |
| 315 | do_disable = true; |
| 316 | break; |
| 317 | case 'l': |
| 318 | do_dump = true; |
| 319 | break; |
| 320 | case 'h': |
| 321 | do_usage = true; |
| 322 | break; |
| 323 | default: |
| 324 | do_usage = true; |
| 325 | ret = 1; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (do_usage) { |
| 330 | print_usage(); |
| 331 | } else if (do_dump) { |
| 332 | dump_info(); |
| 333 | } else if (intel_pipe) { |
| 334 | if (do_disable) |
| 335 | ret = disable_panel_fitter(intel_pipe - 'A'); |
| 336 | else |
| 337 | ret = change_screen_size(intel_pipe - 'A', x, y); |
| 338 | } else { |
| 339 | print_usage(); |
| 340 | ret = 1; |
| 341 | } |
| 342 | |
| 343 | out: |
| 344 | intel_register_access_fini(); |
| 345 | return ret; |
| 346 | } |