1 /*
2 * Copyright © 2008 Intel Corporation
3 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 *
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <err.h>
39 #include <assert.h>
40 #include <sys/ioctl.h>
41 #include <fcntl.h>
42 #include <sys/stat.h>
43 #include <sys/mman.h>
44 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM
45 #include <sys/sysinfo.h>
46 #elif defined(HAVE_SWAPCTL) /* Solaris */
47 #include <sys/swap.h>
48 #endif
49
50 #include "intel_gpu_tools.h"
51 #include "i915_drm.h"
52
53 uint32_t
intel_get_drm_devid(int fd)54 intel_get_drm_devid(int fd)
55 {
56 int ret;
57 struct drm_i915_getparam gp;
58 uint32_t devid;
59 char *override;
60
61 override = getenv("INTEL_DEVID_OVERRIDE");
62 if (override) {
63 devid = strtod(override, NULL);
64 } else {
65 gp.param = I915_PARAM_CHIPSET_ID;
66 gp.value = (int *)&devid;
67
68 ret = ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
69 assert(ret == 0);
70 }
71
72 return devid;
73 }
74
intel_gen(uint32_t devid)75 int intel_gen(uint32_t devid)
76 {
77 if (IS_GEN2(devid))
78 return 2;
79 if (IS_GEN3(devid))
80 return 3;
81 if (IS_GEN4(devid))
82 return 4;
83 if (IS_GEN5(devid))
84 return 5;
85 if (IS_GEN6(devid))
86 return 6;
87 if (IS_GEN7(devid))
88 return 7;
89
90 return -1;
91 }
92
93 uint64_t
intel_get_total_ram_mb(void)94 intel_get_total_ram_mb(void)
95 {
96 uint64_t retval;
97
98 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
99 struct sysinfo sysinf;
100 int ret;
101
102 ret = sysinfo(&sysinf);
103 assert(ret == 0);
104
105 retval = sysinf.totalram;
106 retval *= sysinf.mem_unit;
107 #elif defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) /* Solaris */
108 long pagesize, npages;
109
110 pagesize = sysconf(_SC_PAGESIZE);
111 npages = sysconf(_SC_PHYS_PAGES);
112
113 retval = (uint64_t) pagesize * npages;
114 #else
115 #error "Unknown how to get RAM size for this OS"
116 #endif
117
118 return retval / (1024*1024);
119 }
120
121 uint64_t
intel_get_total_swap_mb(void)122 intel_get_total_swap_mb(void)
123 {
124 uint64_t retval;
125
126 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
127 struct sysinfo sysinf;
128 int ret;
129
130 ret = sysinfo(&sysinf);
131 assert(ret == 0);
132
133 retval = sysinf.totalswap;
134 retval *= sysinf.mem_unit;
135 #elif defined(HAVE_SWAPCTL) /* Solaris */
136 long pagesize = sysconf(_SC_PAGESIZE);
137 uint64_t totalpages = 0;
138 swaptbl_t *swt;
139 char *buf;
140 int n, i;
141
142 if ((n = swapctl(SC_GETNSWP, NULL)) == -1) {
143 perror("swapctl: GETNSWP");
144 return 0;
145 }
146 if (n == 0) {
147 /* no error, but no swap devices either */
148 return 0;
149 }
150
151 swt = malloc(sizeof(struct swaptable) + (n * sizeof(swapent_t)));
152 buf = malloc(n * MAXPATHLEN);
153 if (!swt || !buf) {
154 perror("malloc");
155 } else {
156 swt->swt_n = n;
157 for (i = 0 ; i < n; i++) {
158 swt->swt_ent[i].ste_path = buf + (i * MAXPATHLEN);
159 }
160
161 if ((n = swapctl(SC_LIST, swt)) == -1) {
162 perror("swapctl: LIST");
163 } else {
164 for (i = 0; i < swt->swt_n; i++) {
165 totalpages += swt->swt_ent[i].ste_pages;
166 }
167 }
168 }
169 free(swt);
170 free(buf);
171
172 retval = (uint64_t) pagesize * totalpages;
173 #else
174 #warning "Unknown how to get swap size for this OS"
175 return 0;
176 #endif
177
178 return retval / (1024*1024);
179 }
180
181
182 /*
183 * When testing a port to a new platform, create a standalone test binary
184 * by running:
185 * cc -o porttest intel_drm.c -I.. -DSTANDALONE_TEST `pkg-config --cflags libdrm`
186 * and then running the resulting porttest program.
187 */
188 #ifdef STANDALONE_TEST
189 void *mmio;
190
main(int argc,char ** argv)191 int main(int argc, char **argv)
192 {
193 printf("Total RAM: %" PRIu64 " Mb\n", intel_get_total_ram_mb());
194 printf("Total Swap: %" PRIu64 " Mb\n", intel_get_total_swap_mb());
195
196 return 0;
197 }
198 #endif /* STANDALONE_TEST */
199