Home | History | Annotate | Download | only in msm
      1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
      2 
      3 /*
      4  * Copyright (C) 2013 Rob Clark <robclark (at) freedesktop.org>
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  * SOFTWARE.
     24  *
     25  * Authors:
     26  *    Rob Clark <robclark (at) freedesktop.org>
     27  */
     28 
     29 #ifdef HAVE_CONFIG_H
     30 # include <config.h>
     31 #endif
     32 
     33 #include "msm_priv.h"
     34 
     35 
     36 static int msm_pipe_get_param(struct fd_pipe *pipe,
     37 		enum fd_param_id param, uint64_t *value)
     38 {
     39 	struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
     40 	switch(param) {
     41 	case FD_DEVICE_ID: // XXX probably get rid of this..
     42 	case FD_GPU_ID:
     43 		*value = msm_pipe->gpu_id;
     44 		return 0;
     45 	case FD_GMEM_SIZE:
     46 		*value = msm_pipe->gmem;
     47 		return 0;
     48 	case FD_CHIP_ID:
     49 		*value = msm_pipe->chip_id;
     50 		return 0;
     51 	default:
     52 		ERROR_MSG("invalid param id: %d", param);
     53 		return -1;
     54 	}
     55 }
     56 
     57 static int msm_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp)
     58 {
     59 	struct fd_device *dev = pipe->dev;
     60 	struct drm_msm_wait_fence req = {
     61 			.fence = timestamp,
     62 	};
     63 	int ret;
     64 
     65 	get_abs_timeout(&req.timeout, 5000);
     66 
     67 	ret = drmCommandWrite(dev->fd, DRM_MSM_WAIT_FENCE, &req, sizeof(req));
     68 	if (ret) {
     69 		ERROR_MSG("wait-fence failed! %d (%s)", ret, strerror(errno));
     70 		return ret;
     71 	}
     72 
     73 	return 0;
     74 }
     75 
     76 static void msm_pipe_destroy(struct fd_pipe *pipe)
     77 {
     78 	struct msm_pipe *msm_pipe = to_msm_pipe(pipe);
     79 	free(msm_pipe);
     80 }
     81 
     82 static struct fd_pipe_funcs funcs = {
     83 		.ringbuffer_new = msm_ringbuffer_new,
     84 		.get_param = msm_pipe_get_param,
     85 		.wait = msm_pipe_wait,
     86 		.destroy = msm_pipe_destroy,
     87 };
     88 
     89 static uint64_t get_param(struct fd_device *dev, uint32_t pipe, uint32_t param)
     90 {
     91 	struct drm_msm_param req = {
     92 			.pipe = pipe,
     93 			.param = param,
     94 	};
     95 	int ret;
     96 
     97 	ret = drmCommandWriteRead(dev->fd, DRM_MSM_GET_PARAM, &req, sizeof(req));
     98 	if (ret) {
     99 		ERROR_MSG("get-param failed! %d (%s)", ret, strerror(errno));
    100 		return 0;
    101 	}
    102 
    103 	return req.value;
    104 }
    105 
    106 struct fd_pipe * msm_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
    107 {
    108 	static const uint32_t pipe_id[] = {
    109 			[FD_PIPE_3D] = MSM_PIPE_3D0,
    110 			[FD_PIPE_2D] = MSM_PIPE_2D0,
    111 	};
    112 	struct msm_pipe *msm_pipe = NULL;
    113 	struct fd_pipe *pipe = NULL;
    114 
    115 	msm_pipe = calloc(1, sizeof(*msm_pipe));
    116 	if (!msm_pipe) {
    117 		ERROR_MSG("allocation failed");
    118 		goto fail;
    119 	}
    120 
    121 	pipe = &msm_pipe->base;
    122 	pipe->funcs = &funcs;
    123 
    124 	msm_pipe->pipe = pipe_id[id];
    125 	msm_pipe->gpu_id = get_param(dev, pipe_id[id], MSM_PARAM_GPU_ID);
    126 	msm_pipe->gmem   = get_param(dev, pipe_id[id], MSM_PARAM_GMEM_SIZE);
    127 	msm_pipe->chip_id = get_param(dev, pipe_id[id], MSM_PARAM_CHIP_ID);
    128 
    129 	if (! msm_pipe->gpu_id)
    130 		goto fail;
    131 
    132 	INFO_MSG("Pipe Info:");
    133 	INFO_MSG(" GPU-id:          %d", msm_pipe->gpu_id);
    134 	INFO_MSG(" Chip-id:         0x%08x", msm_pipe->chip_id);
    135 	INFO_MSG(" GMEM size:       0x%08x", msm_pipe->gmem);
    136 
    137 	return pipe;
    138 fail:
    139 	if (pipe)
    140 		fd_pipe_del(pipe);
    141 	return NULL;
    142 }
    143