/* * Copyright © 2012 Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. * * Authors: * Ben Widawsky * Jeff McGee * */ #define _GNU_SOURCE #include #include #include #include #include "drmtest.h" static bool verbose = false; static int origmin, origmax; static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz"; enum { CUR, MIN, MAX, RP0, RP1, RPn }; struct junk { const char *name; const char *mode; FILE *filp; } stuff[] = { { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL } }; static int readval(FILE *filp) { int val; int scanned; rewind(filp); scanned = fscanf(filp, "%d", &val); igt_assert(scanned == 1); return val; } static int do_writeval(FILE *filp, int val, int lerrno) { int ret, orig; orig = readval(filp); rewind(filp); ret = fprintf(filp, "%d", val); if (lerrno) { /* Expecting specific error */ igt_assert(ret == EOF && errno == lerrno); igt_assert(readval(filp) == orig); } else { /* Expecting no error */ igt_assert(ret != EOF); igt_assert(readval(filp) == val); } return ret; } #define writeval(filp, val) do_writeval(filp, val, 0) #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL) #define fcur (readval(stuff[CUR].filp)) #define fmin (readval(stuff[MIN].filp)) #define fmax (readval(stuff[MAX].filp)) #define frp0 (readval(stuff[RP0].filp)) #define frp1 (readval(stuff[RP1].filp)) #define frpn (readval(stuff[RPn].filp)) static void setfreq(int val) { if (val > fmax) { writeval(stuff[MAX].filp, val); writeval(stuff[MIN].filp, val); } else { writeval(stuff[MIN].filp, val); writeval(stuff[MAX].filp, val); } } static void checkit(void) { igt_assert(fmin <= fmax); igt_assert(fcur <= fmax); igt_assert(fmin <= fcur); igt_assert(frpn <= fmin); igt_assert(fmax <= frp0); igt_assert(frp1 <= frp0); igt_assert(frpn <= frp1); igt_assert(frp0 != 0); igt_assert(frp1 != 0); } static void dumpit(void) { struct junk *junk = stuff; do { printf("gt frequency %s (MHz): %d\n", junk->name, readval(junk->filp)); junk++; } while(junk->name != NULL); printf("\n"); } static void pm_rps_exit_handler(int sig) { if (origmin > fmax) { writeval(stuff[MAX].filp, origmax); writeval(stuff[MIN].filp, origmin); } else { writeval(stuff[MIN].filp, origmin); writeval(stuff[MAX].filp, origmax); } } igt_simple_main { const int device = drm_get_card(); struct junk *junk = stuff; int fd, ret; igt_skip_on_simulation(); /* Use drm_open_any to verify device existence */ fd = drm_open_any(); close(fd); do { int val = -1; char *path; ret = asprintf(&path, sysfs_base_path, device, junk->name); igt_assert(ret != -1); junk->filp = fopen(path, junk->mode); igt_require(junk->filp); setbuf(junk->filp, NULL); val = readval(junk->filp); igt_assert(val >= 0); junk++; } while(junk->name != NULL); origmin = fmin; origmax = fmax; igt_install_exit_handler(pm_rps_exit_handler); if (verbose) printf("Original min = %d\nOriginal max = %d\n", origmin, origmax); if (verbose) dumpit(); checkit(); setfreq(origmin); if (verbose) dumpit(); igt_assert(fcur == fmin); setfreq(origmax); if (verbose) dumpit(); igt_assert(fcur == fmax); checkit(); /* And some errors */ writeval_inval(stuff[MIN].filp, frpn - 1); writeval_inval(stuff[MAX].filp, frp0 + 1000); checkit(); writeval_inval(stuff[MIN].filp, fmax + 1000); writeval_inval(stuff[MAX].filp, fmin - 1); checkit(); writeval_inval(stuff[MIN].filp, 0x11111110); writeval_inval(stuff[MAX].filp, 0); writeval(stuff[MIN].filp, origmin); writeval(stuff[MAX].filp, origmax); }