i915_gem.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017
  1. /*
  2. * Copyright © 2008 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 DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj);
  37. static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
  38. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  39. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  40. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  41. int write);
  42. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  43. uint64_t offset,
  44. uint64_t size);
  45. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  46. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  47. static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  48. unsigned alignment);
  49. static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
  50. static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  51. struct drm_i915_gem_pwrite *args,
  52. struct drm_file *file_priv);
  53. static void i915_gem_free_object_tail(struct drm_gem_object *obj);
  54. static LIST_HEAD(shrink_list);
  55. static DEFINE_SPINLOCK(shrink_list_lock);
  56. static inline bool
  57. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
  58. {
  59. return obj_priv->gtt_space &&
  60. !obj_priv->active &&
  61. obj_priv->pin_count == 0;
  62. }
  63. int i915_gem_do_init(struct drm_device *dev, unsigned long start,
  64. unsigned long end)
  65. {
  66. drm_i915_private_t *dev_priv = dev->dev_private;
  67. if (start >= end ||
  68. (start & (PAGE_SIZE - 1)) != 0 ||
  69. (end & (PAGE_SIZE - 1)) != 0) {
  70. return -EINVAL;
  71. }
  72. drm_mm_init(&dev_priv->mm.gtt_space, start,
  73. end - start);
  74. dev->gtt_total = (uint32_t) (end - start);
  75. return 0;
  76. }
  77. int
  78. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  79. struct drm_file *file_priv)
  80. {
  81. struct drm_i915_gem_init *args = data;
  82. int ret;
  83. mutex_lock(&dev->struct_mutex);
  84. ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
  85. mutex_unlock(&dev->struct_mutex);
  86. return ret;
  87. }
  88. int
  89. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  90. struct drm_file *file_priv)
  91. {
  92. struct drm_i915_gem_get_aperture *args = data;
  93. if (!(dev->driver->driver_features & DRIVER_GEM))
  94. return -ENODEV;
  95. args->aper_size = dev->gtt_total;
  96. args->aper_available_size = (args->aper_size -
  97. atomic_read(&dev->pin_memory));
  98. return 0;
  99. }
  100. /**
  101. * Creates a new mm object and returns a handle to it.
  102. */
  103. int
  104. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  105. struct drm_file *file_priv)
  106. {
  107. struct drm_i915_gem_create *args = data;
  108. struct drm_gem_object *obj;
  109. int ret;
  110. u32 handle;
  111. args->size = roundup(args->size, PAGE_SIZE);
  112. /* Allocate the new object */
  113. obj = i915_gem_alloc_object(dev, args->size);
  114. if (obj == NULL)
  115. return -ENOMEM;
  116. ret = drm_gem_handle_create(file_priv, obj, &handle);
  117. drm_gem_object_unreference_unlocked(obj);
  118. if (ret)
  119. return ret;
  120. args->handle = handle;
  121. return 0;
  122. }
  123. static inline int
  124. fast_shmem_read(struct page **pages,
  125. loff_t page_base, int page_offset,
  126. char __user *data,
  127. int length)
  128. {
  129. char __iomem *vaddr;
  130. int unwritten;
  131. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  132. if (vaddr == NULL)
  133. return -ENOMEM;
  134. unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
  135. kunmap_atomic(vaddr, KM_USER0);
  136. if (unwritten)
  137. return -EFAULT;
  138. return 0;
  139. }
  140. static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
  141. {
  142. drm_i915_private_t *dev_priv = obj->dev->dev_private;
  143. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  144. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  145. obj_priv->tiling_mode != I915_TILING_NONE;
  146. }
  147. static inline void
  148. slow_shmem_copy(struct page *dst_page,
  149. int dst_offset,
  150. struct page *src_page,
  151. int src_offset,
  152. int length)
  153. {
  154. char *dst_vaddr, *src_vaddr;
  155. dst_vaddr = kmap(dst_page);
  156. src_vaddr = kmap(src_page);
  157. memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
  158. kunmap(src_page);
  159. kunmap(dst_page);
  160. }
  161. static inline void
  162. slow_shmem_bit17_copy(struct page *gpu_page,
  163. int gpu_offset,
  164. struct page *cpu_page,
  165. int cpu_offset,
  166. int length,
  167. int is_read)
  168. {
  169. char *gpu_vaddr, *cpu_vaddr;
  170. /* Use the unswizzled path if this page isn't affected. */
  171. if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
  172. if (is_read)
  173. return slow_shmem_copy(cpu_page, cpu_offset,
  174. gpu_page, gpu_offset, length);
  175. else
  176. return slow_shmem_copy(gpu_page, gpu_offset,
  177. cpu_page, cpu_offset, length);
  178. }
  179. gpu_vaddr = kmap(gpu_page);
  180. cpu_vaddr = kmap(cpu_page);
  181. /* Copy the data, XORing A6 with A17 (1). The user already knows he's
  182. * XORing with the other bits (A9 for Y, A9 and A10 for X)
  183. */
  184. while (length > 0) {
  185. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  186. int this_length = min(cacheline_end - gpu_offset, length);
  187. int swizzled_gpu_offset = gpu_offset ^ 64;
  188. if (is_read) {
  189. memcpy(cpu_vaddr + cpu_offset,
  190. gpu_vaddr + swizzled_gpu_offset,
  191. this_length);
  192. } else {
  193. memcpy(gpu_vaddr + swizzled_gpu_offset,
  194. cpu_vaddr + cpu_offset,
  195. this_length);
  196. }
  197. cpu_offset += this_length;
  198. gpu_offset += this_length;
  199. length -= this_length;
  200. }
  201. kunmap(cpu_page);
  202. kunmap(gpu_page);
  203. }
  204. /**
  205. * This is the fast shmem pread path, which attempts to copy_from_user directly
  206. * from the backing pages of the object to the user's address space. On a
  207. * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
  208. */
  209. static int
  210. i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
  211. struct drm_i915_gem_pread *args,
  212. struct drm_file *file_priv)
  213. {
  214. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  215. ssize_t remain;
  216. loff_t offset, page_base;
  217. char __user *user_data;
  218. int page_offset, page_length;
  219. int ret;
  220. user_data = (char __user *) (uintptr_t) args->data_ptr;
  221. remain = args->size;
  222. mutex_lock(&dev->struct_mutex);
  223. ret = i915_gem_object_get_pages(obj, 0);
  224. if (ret != 0)
  225. goto fail_unlock;
  226. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  227. args->size);
  228. if (ret != 0)
  229. goto fail_put_pages;
  230. obj_priv = to_intel_bo(obj);
  231. offset = args->offset;
  232. while (remain > 0) {
  233. /* Operation in this page
  234. *
  235. * page_base = page offset within aperture
  236. * page_offset = offset within page
  237. * page_length = bytes to copy for this page
  238. */
  239. page_base = (offset & ~(PAGE_SIZE-1));
  240. page_offset = offset & (PAGE_SIZE-1);
  241. page_length = remain;
  242. if ((page_offset + remain) > PAGE_SIZE)
  243. page_length = PAGE_SIZE - page_offset;
  244. ret = fast_shmem_read(obj_priv->pages,
  245. page_base, page_offset,
  246. user_data, page_length);
  247. if (ret)
  248. goto fail_put_pages;
  249. remain -= page_length;
  250. user_data += page_length;
  251. offset += page_length;
  252. }
  253. fail_put_pages:
  254. i915_gem_object_put_pages(obj);
  255. fail_unlock:
  256. mutex_unlock(&dev->struct_mutex);
  257. return ret;
  258. }
  259. static int
  260. i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
  261. {
  262. int ret;
  263. ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
  264. /* If we've insufficient memory to map in the pages, attempt
  265. * to make some space by throwing out some old buffers.
  266. */
  267. if (ret == -ENOMEM) {
  268. struct drm_device *dev = obj->dev;
  269. ret = i915_gem_evict_something(dev, obj->size,
  270. i915_gem_get_gtt_alignment(obj));
  271. if (ret)
  272. return ret;
  273. ret = i915_gem_object_get_pages(obj, 0);
  274. }
  275. return ret;
  276. }
  277. /**
  278. * This is the fallback shmem pread path, which allocates temporary storage
  279. * in kernel space to copy_to_user into outside of the struct_mutex, so we
  280. * can copy out of the object's backing pages while holding the struct mutex
  281. * and not take page faults.
  282. */
  283. static int
  284. i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
  285. struct drm_i915_gem_pread *args,
  286. struct drm_file *file_priv)
  287. {
  288. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  289. struct mm_struct *mm = current->mm;
  290. struct page **user_pages;
  291. ssize_t remain;
  292. loff_t offset, pinned_pages, i;
  293. loff_t first_data_page, last_data_page, num_pages;
  294. int shmem_page_index, shmem_page_offset;
  295. int data_page_index, data_page_offset;
  296. int page_length;
  297. int ret;
  298. uint64_t data_ptr = args->data_ptr;
  299. int do_bit17_swizzling;
  300. remain = args->size;
  301. /* Pin the user pages containing the data. We can't fault while
  302. * holding the struct mutex, yet we want to hold it while
  303. * dereferencing the user data.
  304. */
  305. first_data_page = data_ptr / PAGE_SIZE;
  306. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  307. num_pages = last_data_page - first_data_page + 1;
  308. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  309. if (user_pages == NULL)
  310. return -ENOMEM;
  311. down_read(&mm->mmap_sem);
  312. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  313. num_pages, 1, 0, user_pages, NULL);
  314. up_read(&mm->mmap_sem);
  315. if (pinned_pages < num_pages) {
  316. ret = -EFAULT;
  317. goto fail_put_user_pages;
  318. }
  319. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  320. mutex_lock(&dev->struct_mutex);
  321. ret = i915_gem_object_get_pages_or_evict(obj);
  322. if (ret)
  323. goto fail_unlock;
  324. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  325. args->size);
  326. if (ret != 0)
  327. goto fail_put_pages;
  328. obj_priv = to_intel_bo(obj);
  329. offset = args->offset;
  330. while (remain > 0) {
  331. /* Operation in this page
  332. *
  333. * shmem_page_index = page number within shmem file
  334. * shmem_page_offset = offset within page in shmem file
  335. * data_page_index = page number in get_user_pages return
  336. * data_page_offset = offset with data_page_index page.
  337. * page_length = bytes to copy for this page
  338. */
  339. shmem_page_index = offset / PAGE_SIZE;
  340. shmem_page_offset = offset & ~PAGE_MASK;
  341. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  342. data_page_offset = data_ptr & ~PAGE_MASK;
  343. page_length = remain;
  344. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  345. page_length = PAGE_SIZE - shmem_page_offset;
  346. if ((data_page_offset + page_length) > PAGE_SIZE)
  347. page_length = PAGE_SIZE - data_page_offset;
  348. if (do_bit17_swizzling) {
  349. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  350. shmem_page_offset,
  351. user_pages[data_page_index],
  352. data_page_offset,
  353. page_length,
  354. 1);
  355. } else {
  356. slow_shmem_copy(user_pages[data_page_index],
  357. data_page_offset,
  358. obj_priv->pages[shmem_page_index],
  359. shmem_page_offset,
  360. page_length);
  361. }
  362. remain -= page_length;
  363. data_ptr += page_length;
  364. offset += page_length;
  365. }
  366. fail_put_pages:
  367. i915_gem_object_put_pages(obj);
  368. fail_unlock:
  369. mutex_unlock(&dev->struct_mutex);
  370. fail_put_user_pages:
  371. for (i = 0; i < pinned_pages; i++) {
  372. SetPageDirty(user_pages[i]);
  373. page_cache_release(user_pages[i]);
  374. }
  375. drm_free_large(user_pages);
  376. return ret;
  377. }
  378. /**
  379. * Reads data from the object referenced by handle.
  380. *
  381. * On error, the contents of *data are undefined.
  382. */
  383. int
  384. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  385. struct drm_file *file_priv)
  386. {
  387. struct drm_i915_gem_pread *args = data;
  388. struct drm_gem_object *obj;
  389. struct drm_i915_gem_object *obj_priv;
  390. int ret;
  391. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  392. if (obj == NULL)
  393. return -EBADF;
  394. obj_priv = to_intel_bo(obj);
  395. /* Bounds check source.
  396. *
  397. * XXX: This could use review for overflow issues...
  398. */
  399. if (args->offset > obj->size || args->size > obj->size ||
  400. args->offset + args->size > obj->size) {
  401. drm_gem_object_unreference_unlocked(obj);
  402. return -EINVAL;
  403. }
  404. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  405. ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
  406. } else {
  407. ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
  408. if (ret != 0)
  409. ret = i915_gem_shmem_pread_slow(dev, obj, args,
  410. file_priv);
  411. }
  412. drm_gem_object_unreference_unlocked(obj);
  413. return ret;
  414. }
  415. /* This is the fast write path which cannot handle
  416. * page faults in the source data
  417. */
  418. static inline int
  419. fast_user_write(struct io_mapping *mapping,
  420. loff_t page_base, int page_offset,
  421. char __user *user_data,
  422. int length)
  423. {
  424. char *vaddr_atomic;
  425. unsigned long unwritten;
  426. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base, KM_USER0);
  427. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  428. user_data, length);
  429. io_mapping_unmap_atomic(vaddr_atomic, KM_USER0);
  430. if (unwritten)
  431. return -EFAULT;
  432. return 0;
  433. }
  434. /* Here's the write path which can sleep for
  435. * page faults
  436. */
  437. static inline void
  438. slow_kernel_write(struct io_mapping *mapping,
  439. loff_t gtt_base, int gtt_offset,
  440. struct page *user_page, int user_offset,
  441. int length)
  442. {
  443. char __iomem *dst_vaddr;
  444. char *src_vaddr;
  445. dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
  446. src_vaddr = kmap(user_page);
  447. memcpy_toio(dst_vaddr + gtt_offset,
  448. src_vaddr + user_offset,
  449. length);
  450. kunmap(user_page);
  451. io_mapping_unmap(dst_vaddr);
  452. }
  453. static inline int
  454. fast_shmem_write(struct page **pages,
  455. loff_t page_base, int page_offset,
  456. char __user *data,
  457. int length)
  458. {
  459. char __iomem *vaddr;
  460. unsigned long unwritten;
  461. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  462. if (vaddr == NULL)
  463. return -ENOMEM;
  464. unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
  465. kunmap_atomic(vaddr, KM_USER0);
  466. if (unwritten)
  467. return -EFAULT;
  468. return 0;
  469. }
  470. /**
  471. * This is the fast pwrite path, where we copy the data directly from the
  472. * user into the GTT, uncached.
  473. */
  474. static int
  475. i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  476. struct drm_i915_gem_pwrite *args,
  477. struct drm_file *file_priv)
  478. {
  479. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  480. drm_i915_private_t *dev_priv = dev->dev_private;
  481. ssize_t remain;
  482. loff_t offset, page_base;
  483. char __user *user_data;
  484. int page_offset, page_length;
  485. int ret;
  486. user_data = (char __user *) (uintptr_t) args->data_ptr;
  487. remain = args->size;
  488. if (!access_ok(VERIFY_READ, user_data, remain))
  489. return -EFAULT;
  490. mutex_lock(&dev->struct_mutex);
  491. ret = i915_gem_object_pin(obj, 0);
  492. if (ret) {
  493. mutex_unlock(&dev->struct_mutex);
  494. return ret;
  495. }
  496. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  497. if (ret)
  498. goto fail;
  499. obj_priv = to_intel_bo(obj);
  500. offset = obj_priv->gtt_offset + args->offset;
  501. while (remain > 0) {
  502. /* Operation in this page
  503. *
  504. * page_base = page offset within aperture
  505. * page_offset = offset within page
  506. * page_length = bytes to copy for this page
  507. */
  508. page_base = (offset & ~(PAGE_SIZE-1));
  509. page_offset = offset & (PAGE_SIZE-1);
  510. page_length = remain;
  511. if ((page_offset + remain) > PAGE_SIZE)
  512. page_length = PAGE_SIZE - page_offset;
  513. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  514. page_offset, user_data, page_length);
  515. /* If we get a fault while copying data, then (presumably) our
  516. * source page isn't available. Return the error and we'll
  517. * retry in the slow path.
  518. */
  519. if (ret)
  520. goto fail;
  521. remain -= page_length;
  522. user_data += page_length;
  523. offset += page_length;
  524. }
  525. fail:
  526. i915_gem_object_unpin(obj);
  527. mutex_unlock(&dev->struct_mutex);
  528. return ret;
  529. }
  530. /**
  531. * This is the fallback GTT pwrite path, which uses get_user_pages to pin
  532. * the memory and maps it using kmap_atomic for copying.
  533. *
  534. * This code resulted in x11perf -rgb10text consuming about 10% more CPU
  535. * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
  536. */
  537. static int
  538. i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  539. struct drm_i915_gem_pwrite *args,
  540. struct drm_file *file_priv)
  541. {
  542. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  543. drm_i915_private_t *dev_priv = dev->dev_private;
  544. ssize_t remain;
  545. loff_t gtt_page_base, offset;
  546. loff_t first_data_page, last_data_page, num_pages;
  547. loff_t pinned_pages, i;
  548. struct page **user_pages;
  549. struct mm_struct *mm = current->mm;
  550. int gtt_page_offset, data_page_offset, data_page_index, page_length;
  551. int ret;
  552. uint64_t data_ptr = args->data_ptr;
  553. remain = args->size;
  554. /* Pin the user pages containing the data. We can't fault while
  555. * holding the struct mutex, and all of the pwrite implementations
  556. * want to hold it while dereferencing the user data.
  557. */
  558. first_data_page = data_ptr / PAGE_SIZE;
  559. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  560. num_pages = last_data_page - first_data_page + 1;
  561. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  562. if (user_pages == NULL)
  563. return -ENOMEM;
  564. down_read(&mm->mmap_sem);
  565. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  566. num_pages, 0, 0, user_pages, NULL);
  567. up_read(&mm->mmap_sem);
  568. if (pinned_pages < num_pages) {
  569. ret = -EFAULT;
  570. goto out_unpin_pages;
  571. }
  572. mutex_lock(&dev->struct_mutex);
  573. ret = i915_gem_object_pin(obj, 0);
  574. if (ret)
  575. goto out_unlock;
  576. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  577. if (ret)
  578. goto out_unpin_object;
  579. obj_priv = to_intel_bo(obj);
  580. offset = obj_priv->gtt_offset + args->offset;
  581. while (remain > 0) {
  582. /* Operation in this page
  583. *
  584. * gtt_page_base = page offset within aperture
  585. * gtt_page_offset = offset within page in aperture
  586. * data_page_index = page number in get_user_pages return
  587. * data_page_offset = offset with data_page_index page.
  588. * page_length = bytes to copy for this page
  589. */
  590. gtt_page_base = offset & PAGE_MASK;
  591. gtt_page_offset = offset & ~PAGE_MASK;
  592. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  593. data_page_offset = data_ptr & ~PAGE_MASK;
  594. page_length = remain;
  595. if ((gtt_page_offset + page_length) > PAGE_SIZE)
  596. page_length = PAGE_SIZE - gtt_page_offset;
  597. if ((data_page_offset + page_length) > PAGE_SIZE)
  598. page_length = PAGE_SIZE - data_page_offset;
  599. slow_kernel_write(dev_priv->mm.gtt_mapping,
  600. gtt_page_base, gtt_page_offset,
  601. user_pages[data_page_index],
  602. data_page_offset,
  603. page_length);
  604. remain -= page_length;
  605. offset += page_length;
  606. data_ptr += page_length;
  607. }
  608. out_unpin_object:
  609. i915_gem_object_unpin(obj);
  610. out_unlock:
  611. mutex_unlock(&dev->struct_mutex);
  612. out_unpin_pages:
  613. for (i = 0; i < pinned_pages; i++)
  614. page_cache_release(user_pages[i]);
  615. drm_free_large(user_pages);
  616. return ret;
  617. }
  618. /**
  619. * This is the fast shmem pwrite path, which attempts to directly
  620. * copy_from_user into the kmapped pages backing the object.
  621. */
  622. static int
  623. i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  624. struct drm_i915_gem_pwrite *args,
  625. struct drm_file *file_priv)
  626. {
  627. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  628. ssize_t remain;
  629. loff_t offset, page_base;
  630. char __user *user_data;
  631. int page_offset, page_length;
  632. int ret;
  633. user_data = (char __user *) (uintptr_t) args->data_ptr;
  634. remain = args->size;
  635. mutex_lock(&dev->struct_mutex);
  636. ret = i915_gem_object_get_pages(obj, 0);
  637. if (ret != 0)
  638. goto fail_unlock;
  639. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  640. if (ret != 0)
  641. goto fail_put_pages;
  642. obj_priv = to_intel_bo(obj);
  643. offset = args->offset;
  644. obj_priv->dirty = 1;
  645. while (remain > 0) {
  646. /* Operation in this page
  647. *
  648. * page_base = page offset within aperture
  649. * page_offset = offset within page
  650. * page_length = bytes to copy for this page
  651. */
  652. page_base = (offset & ~(PAGE_SIZE-1));
  653. page_offset = offset & (PAGE_SIZE-1);
  654. page_length = remain;
  655. if ((page_offset + remain) > PAGE_SIZE)
  656. page_length = PAGE_SIZE - page_offset;
  657. ret = fast_shmem_write(obj_priv->pages,
  658. page_base, page_offset,
  659. user_data, page_length);
  660. if (ret)
  661. goto fail_put_pages;
  662. remain -= page_length;
  663. user_data += page_length;
  664. offset += page_length;
  665. }
  666. fail_put_pages:
  667. i915_gem_object_put_pages(obj);
  668. fail_unlock:
  669. mutex_unlock(&dev->struct_mutex);
  670. return ret;
  671. }
  672. /**
  673. * This is the fallback shmem pwrite path, which uses get_user_pages to pin
  674. * the memory and maps it using kmap_atomic for copying.
  675. *
  676. * This avoids taking mmap_sem for faulting on the user's address while the
  677. * struct_mutex is held.
  678. */
  679. static int
  680. i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  681. struct drm_i915_gem_pwrite *args,
  682. struct drm_file *file_priv)
  683. {
  684. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  685. struct mm_struct *mm = current->mm;
  686. struct page **user_pages;
  687. ssize_t remain;
  688. loff_t offset, pinned_pages, i;
  689. loff_t first_data_page, last_data_page, num_pages;
  690. int shmem_page_index, shmem_page_offset;
  691. int data_page_index, data_page_offset;
  692. int page_length;
  693. int ret;
  694. uint64_t data_ptr = args->data_ptr;
  695. int do_bit17_swizzling;
  696. remain = args->size;
  697. /* Pin the user pages containing the data. We can't fault while
  698. * holding the struct mutex, and all of the pwrite implementations
  699. * want to hold it while dereferencing the user data.
  700. */
  701. first_data_page = data_ptr / PAGE_SIZE;
  702. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  703. num_pages = last_data_page - first_data_page + 1;
  704. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  705. if (user_pages == NULL)
  706. return -ENOMEM;
  707. down_read(&mm->mmap_sem);
  708. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  709. num_pages, 0, 0, user_pages, NULL);
  710. up_read(&mm->mmap_sem);
  711. if (pinned_pages < num_pages) {
  712. ret = -EFAULT;
  713. goto fail_put_user_pages;
  714. }
  715. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  716. mutex_lock(&dev->struct_mutex);
  717. ret = i915_gem_object_get_pages_or_evict(obj);
  718. if (ret)
  719. goto fail_unlock;
  720. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  721. if (ret != 0)
  722. goto fail_put_pages;
  723. obj_priv = to_intel_bo(obj);
  724. offset = args->offset;
  725. obj_priv->dirty = 1;
  726. while (remain > 0) {
  727. /* Operation in this page
  728. *
  729. * shmem_page_index = page number within shmem file
  730. * shmem_page_offset = offset within page in shmem file
  731. * data_page_index = page number in get_user_pages return
  732. * data_page_offset = offset with data_page_index page.
  733. * page_length = bytes to copy for this page
  734. */
  735. shmem_page_index = offset / PAGE_SIZE;
  736. shmem_page_offset = offset & ~PAGE_MASK;
  737. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  738. data_page_offset = data_ptr & ~PAGE_MASK;
  739. page_length = remain;
  740. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  741. page_length = PAGE_SIZE - shmem_page_offset;
  742. if ((data_page_offset + page_length) > PAGE_SIZE)
  743. page_length = PAGE_SIZE - data_page_offset;
  744. if (do_bit17_swizzling) {
  745. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  746. shmem_page_offset,
  747. user_pages[data_page_index],
  748. data_page_offset,
  749. page_length,
  750. 0);
  751. } else {
  752. slow_shmem_copy(obj_priv->pages[shmem_page_index],
  753. shmem_page_offset,
  754. user_pages[data_page_index],
  755. data_page_offset,
  756. page_length);
  757. }
  758. remain -= page_length;
  759. data_ptr += page_length;
  760. offset += page_length;
  761. }
  762. fail_put_pages:
  763. i915_gem_object_put_pages(obj);
  764. fail_unlock:
  765. mutex_unlock(&dev->struct_mutex);
  766. fail_put_user_pages:
  767. for (i = 0; i < pinned_pages; i++)
  768. page_cache_release(user_pages[i]);
  769. drm_free_large(user_pages);
  770. return ret;
  771. }
  772. /**
  773. * Writes data to the object referenced by handle.
  774. *
  775. * On error, the contents of the buffer that were to be modified are undefined.
  776. */
  777. int
  778. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  779. struct drm_file *file_priv)
  780. {
  781. struct drm_i915_gem_pwrite *args = data;
  782. struct drm_gem_object *obj;
  783. struct drm_i915_gem_object *obj_priv;
  784. int ret = 0;
  785. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  786. if (obj == NULL)
  787. return -EBADF;
  788. obj_priv = to_intel_bo(obj);
  789. /* Bounds check destination.
  790. *
  791. * XXX: This could use review for overflow issues...
  792. */
  793. if (args->offset > obj->size || args->size > obj->size ||
  794. args->offset + args->size > obj->size) {
  795. drm_gem_object_unreference_unlocked(obj);
  796. return -EINVAL;
  797. }
  798. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  799. * it would end up going through the fenced access, and we'll get
  800. * different detiling behavior between reading and writing.
  801. * pread/pwrite currently are reading and writing from the CPU
  802. * perspective, requiring manual detiling by the client.
  803. */
  804. if (obj_priv->phys_obj)
  805. ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
  806. else if (obj_priv->tiling_mode == I915_TILING_NONE &&
  807. dev->gtt_total != 0 &&
  808. obj->write_domain != I915_GEM_DOMAIN_CPU) {
  809. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
  810. if (ret == -EFAULT) {
  811. ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
  812. file_priv);
  813. }
  814. } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
  815. ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
  816. } else {
  817. ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
  818. if (ret == -EFAULT) {
  819. ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
  820. file_priv);
  821. }
  822. }
  823. #if WATCH_PWRITE
  824. if (ret)
  825. DRM_INFO("pwrite failed %d\n", ret);
  826. #endif
  827. drm_gem_object_unreference_unlocked(obj);
  828. return ret;
  829. }
  830. /**
  831. * Called when user space prepares to use an object with the CPU, either
  832. * through the mmap ioctl's mapping or a GTT mapping.
  833. */
  834. int
  835. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  836. struct drm_file *file_priv)
  837. {
  838. struct drm_i915_private *dev_priv = dev->dev_private;
  839. struct drm_i915_gem_set_domain *args = data;
  840. struct drm_gem_object *obj;
  841. struct drm_i915_gem_object *obj_priv;
  842. uint32_t read_domains = args->read_domains;
  843. uint32_t write_domain = args->write_domain;
  844. int ret;
  845. if (!(dev->driver->driver_features & DRIVER_GEM))
  846. return -ENODEV;
  847. /* Only handle setting domains to types used by the CPU. */
  848. if (write_domain & I915_GEM_GPU_DOMAINS)
  849. return -EINVAL;
  850. if (read_domains & I915_GEM_GPU_DOMAINS)
  851. return -EINVAL;
  852. /* Having something in the write domain implies it's in the read
  853. * domain, and only that read domain. Enforce that in the request.
  854. */
  855. if (write_domain != 0 && read_domains != write_domain)
  856. return -EINVAL;
  857. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  858. if (obj == NULL)
  859. return -EBADF;
  860. obj_priv = to_intel_bo(obj);
  861. mutex_lock(&dev->struct_mutex);
  862. intel_mark_busy(dev, obj);
  863. #if WATCH_BUF
  864. DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
  865. obj, obj->size, read_domains, write_domain);
  866. #endif
  867. if (read_domains & I915_GEM_DOMAIN_GTT) {
  868. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  869. /* Update the LRU on the fence for the CPU access that's
  870. * about to occur.
  871. */
  872. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  873. struct drm_i915_fence_reg *reg =
  874. &dev_priv->fence_regs[obj_priv->fence_reg];
  875. list_move_tail(&reg->lru_list,
  876. &dev_priv->mm.fence_list);
  877. }
  878. /* Silently promote "you're not bound, there was nothing to do"
  879. * to success, since the client was just asking us to
  880. * make sure everything was done.
  881. */
  882. if (ret == -EINVAL)
  883. ret = 0;
  884. } else {
  885. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  886. }
  887. /* Maintain LRU order of "inactive" objects */
  888. if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
  889. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  890. drm_gem_object_unreference(obj);
  891. mutex_unlock(&dev->struct_mutex);
  892. return ret;
  893. }
  894. /**
  895. * Called when user space has done writes to this buffer
  896. */
  897. int
  898. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  899. struct drm_file *file_priv)
  900. {
  901. struct drm_i915_gem_sw_finish *args = data;
  902. struct drm_gem_object *obj;
  903. struct drm_i915_gem_object *obj_priv;
  904. int ret = 0;
  905. if (!(dev->driver->driver_features & DRIVER_GEM))
  906. return -ENODEV;
  907. mutex_lock(&dev->struct_mutex);
  908. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  909. if (obj == NULL) {
  910. mutex_unlock(&dev->struct_mutex);
  911. return -EBADF;
  912. }
  913. #if WATCH_BUF
  914. DRM_INFO("%s: sw_finish %d (%p %zd)\n",
  915. __func__, args->handle, obj, obj->size);
  916. #endif
  917. obj_priv = to_intel_bo(obj);
  918. /* Pinned buffers may be scanout, so flush the cache */
  919. if (obj_priv->pin_count)
  920. i915_gem_object_flush_cpu_write_domain(obj);
  921. drm_gem_object_unreference(obj);
  922. mutex_unlock(&dev->struct_mutex);
  923. return ret;
  924. }
  925. /**
  926. * Maps the contents of an object, returning the address it is mapped
  927. * into.
  928. *
  929. * While the mapping holds a reference on the contents of the object, it doesn't
  930. * imply a ref on the object itself.
  931. */
  932. int
  933. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  934. struct drm_file *file_priv)
  935. {
  936. struct drm_i915_gem_mmap *args = data;
  937. struct drm_gem_object *obj;
  938. loff_t offset;
  939. unsigned long addr;
  940. if (!(dev->driver->driver_features & DRIVER_GEM))
  941. return -ENODEV;
  942. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  943. if (obj == NULL)
  944. return -EBADF;
  945. offset = args->offset;
  946. down_write(&current->mm->mmap_sem);
  947. addr = do_mmap(obj->filp, 0, args->size,
  948. PROT_READ | PROT_WRITE, MAP_SHARED,
  949. args->offset);
  950. up_write(&current->mm->mmap_sem);
  951. drm_gem_object_unreference_unlocked(obj);
  952. if (IS_ERR((void *)addr))
  953. return addr;
  954. args->addr_ptr = (uint64_t) addr;
  955. return 0;
  956. }
  957. /**
  958. * i915_gem_fault - fault a page into the GTT
  959. * vma: VMA in question
  960. * vmf: fault info
  961. *
  962. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  963. * from userspace. The fault handler takes care of binding the object to
  964. * the GTT (if needed), allocating and programming a fence register (again,
  965. * only if needed based on whether the old reg is still valid or the object
  966. * is tiled) and inserting a new PTE into the faulting process.
  967. *
  968. * Note that the faulting process may involve evicting existing objects
  969. * from the GTT and/or fence registers to make room. So performance may
  970. * suffer if the GTT working set is large or there are few fence registers
  971. * left.
  972. */
  973. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  974. {
  975. struct drm_gem_object *obj = vma->vm_private_data;
  976. struct drm_device *dev = obj->dev;
  977. drm_i915_private_t *dev_priv = dev->dev_private;
  978. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  979. pgoff_t page_offset;
  980. unsigned long pfn;
  981. int ret = 0;
  982. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  983. /* We don't use vmf->pgoff since that has the fake offset */
  984. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  985. PAGE_SHIFT;
  986. /* Now bind it into the GTT if needed */
  987. mutex_lock(&dev->struct_mutex);
  988. if (!obj_priv->gtt_space) {
  989. ret = i915_gem_object_bind_to_gtt(obj, 0);
  990. if (ret)
  991. goto unlock;
  992. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  993. if (ret)
  994. goto unlock;
  995. }
  996. /* Need a new fence register? */
  997. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  998. ret = i915_gem_object_get_fence_reg(obj);
  999. if (ret)
  1000. goto unlock;
  1001. }
  1002. if (i915_gem_object_is_inactive(obj_priv))
  1003. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1004. pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
  1005. page_offset;
  1006. /* Finally, remap it using the new GTT offset */
  1007. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1008. unlock:
  1009. mutex_unlock(&dev->struct_mutex);
  1010. switch (ret) {
  1011. case 0:
  1012. case -ERESTARTSYS:
  1013. return VM_FAULT_NOPAGE;
  1014. case -ENOMEM:
  1015. case -EAGAIN:
  1016. return VM_FAULT_OOM;
  1017. default:
  1018. return VM_FAULT_SIGBUS;
  1019. }
  1020. }
  1021. /**
  1022. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  1023. * @obj: obj in question
  1024. *
  1025. * GEM memory mapping works by handing back to userspace a fake mmap offset
  1026. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  1027. * up the object based on the offset and sets up the various memory mapping
  1028. * structures.
  1029. *
  1030. * This routine allocates and attaches a fake offset for @obj.
  1031. */
  1032. static int
  1033. i915_gem_create_mmap_offset(struct drm_gem_object *obj)
  1034. {
  1035. struct drm_device *dev = obj->dev;
  1036. struct drm_gem_mm *mm = dev->mm_private;
  1037. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1038. struct drm_map_list *list;
  1039. struct drm_local_map *map;
  1040. int ret = 0;
  1041. /* Set the object up for mmap'ing */
  1042. list = &obj->map_list;
  1043. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  1044. if (!list->map)
  1045. return -ENOMEM;
  1046. map = list->map;
  1047. map->type = _DRM_GEM;
  1048. map->size = obj->size;
  1049. map->handle = obj;
  1050. /* Get a DRM GEM mmap offset allocated... */
  1051. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  1052. obj->size / PAGE_SIZE, 0, 0);
  1053. if (!list->file_offset_node) {
  1054. DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
  1055. ret = -ENOMEM;
  1056. goto out_free_list;
  1057. }
  1058. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  1059. obj->size / PAGE_SIZE, 0);
  1060. if (!list->file_offset_node) {
  1061. ret = -ENOMEM;
  1062. goto out_free_list;
  1063. }
  1064. list->hash.key = list->file_offset_node->start;
  1065. if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
  1066. DRM_ERROR("failed to add to map hash\n");
  1067. ret = -ENOMEM;
  1068. goto out_free_mm;
  1069. }
  1070. /* By now we should be all set, any drm_mmap request on the offset
  1071. * below will get to our mmap & fault handler */
  1072. obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
  1073. return 0;
  1074. out_free_mm:
  1075. drm_mm_put_block(list->file_offset_node);
  1076. out_free_list:
  1077. kfree(list->map);
  1078. return ret;
  1079. }
  1080. /**
  1081. * i915_gem_release_mmap - remove physical page mappings
  1082. * @obj: obj in question
  1083. *
  1084. * Preserve the reservation of the mmapping with the DRM core code, but
  1085. * relinquish ownership of the pages back to the system.
  1086. *
  1087. * It is vital that we remove the page mapping if we have mapped a tiled
  1088. * object through the GTT and then lose the fence register due to
  1089. * resource pressure. Similarly if the object has been moved out of the
  1090. * aperture, than pages mapped into userspace must be revoked. Removing the
  1091. * mapping will then trigger a page fault on the next user access, allowing
  1092. * fixup by i915_gem_fault().
  1093. */
  1094. void
  1095. i915_gem_release_mmap(struct drm_gem_object *obj)
  1096. {
  1097. struct drm_device *dev = obj->dev;
  1098. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1099. if (dev->dev_mapping)
  1100. unmap_mapping_range(dev->dev_mapping,
  1101. obj_priv->mmap_offset, obj->size, 1);
  1102. }
  1103. static void
  1104. i915_gem_free_mmap_offset(struct drm_gem_object *obj)
  1105. {
  1106. struct drm_device *dev = obj->dev;
  1107. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1108. struct drm_gem_mm *mm = dev->mm_private;
  1109. struct drm_map_list *list;
  1110. list = &obj->map_list;
  1111. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  1112. if (list->file_offset_node) {
  1113. drm_mm_put_block(list->file_offset_node);
  1114. list->file_offset_node = NULL;
  1115. }
  1116. if (list->map) {
  1117. kfree(list->map);
  1118. list->map = NULL;
  1119. }
  1120. obj_priv->mmap_offset = 0;
  1121. }
  1122. /**
  1123. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1124. * @obj: object to check
  1125. *
  1126. * Return the required GTT alignment for an object, taking into account
  1127. * potential fence register mapping if needed.
  1128. */
  1129. static uint32_t
  1130. i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
  1131. {
  1132. struct drm_device *dev = obj->dev;
  1133. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1134. int start, i;
  1135. /*
  1136. * Minimum alignment is 4k (GTT page size), but might be greater
  1137. * if a fence register is needed for the object.
  1138. */
  1139. if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
  1140. return 4096;
  1141. /*
  1142. * Previous chips need to be aligned to the size of the smallest
  1143. * fence register that can contain the object.
  1144. */
  1145. if (IS_I9XX(dev))
  1146. start = 1024*1024;
  1147. else
  1148. start = 512*1024;
  1149. for (i = start; i < obj->size; i <<= 1)
  1150. ;
  1151. return i;
  1152. }
  1153. /**
  1154. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1155. * @dev: DRM device
  1156. * @data: GTT mapping ioctl data
  1157. * @file_priv: GEM object info
  1158. *
  1159. * Simply returns the fake offset to userspace so it can mmap it.
  1160. * The mmap call will end up in drm_gem_mmap(), which will set things
  1161. * up so we can get faults in the handler above.
  1162. *
  1163. * The fault handler will take care of binding the object into the GTT
  1164. * (since it may have been evicted to make room for something), allocating
  1165. * a fence register, and mapping the appropriate aperture address into
  1166. * userspace.
  1167. */
  1168. int
  1169. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1170. struct drm_file *file_priv)
  1171. {
  1172. struct drm_i915_gem_mmap_gtt *args = data;
  1173. struct drm_gem_object *obj;
  1174. struct drm_i915_gem_object *obj_priv;
  1175. int ret;
  1176. if (!(dev->driver->driver_features & DRIVER_GEM))
  1177. return -ENODEV;
  1178. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1179. if (obj == NULL)
  1180. return -EBADF;
  1181. mutex_lock(&dev->struct_mutex);
  1182. obj_priv = to_intel_bo(obj);
  1183. if (obj_priv->madv != I915_MADV_WILLNEED) {
  1184. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1185. drm_gem_object_unreference(obj);
  1186. mutex_unlock(&dev->struct_mutex);
  1187. return -EINVAL;
  1188. }
  1189. if (!obj_priv->mmap_offset) {
  1190. ret = i915_gem_create_mmap_offset(obj);
  1191. if (ret) {
  1192. drm_gem_object_unreference(obj);
  1193. mutex_unlock(&dev->struct_mutex);
  1194. return ret;
  1195. }
  1196. }
  1197. args->offset = obj_priv->mmap_offset;
  1198. /*
  1199. * Pull it into the GTT so that we have a page list (makes the
  1200. * initial fault faster and any subsequent flushing possible).
  1201. */
  1202. if (!obj_priv->agp_mem) {
  1203. ret = i915_gem_object_bind_to_gtt(obj, 0);
  1204. if (ret) {
  1205. drm_gem_object_unreference(obj);
  1206. mutex_unlock(&dev->struct_mutex);
  1207. return ret;
  1208. }
  1209. }
  1210. drm_gem_object_unreference(obj);
  1211. mutex_unlock(&dev->struct_mutex);
  1212. return 0;
  1213. }
  1214. void
  1215. i915_gem_object_put_pages(struct drm_gem_object *obj)
  1216. {
  1217. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1218. int page_count = obj->size / PAGE_SIZE;
  1219. int i;
  1220. BUG_ON(obj_priv->pages_refcount == 0);
  1221. BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
  1222. if (--obj_priv->pages_refcount != 0)
  1223. return;
  1224. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1225. i915_gem_object_save_bit_17_swizzle(obj);
  1226. if (obj_priv->madv == I915_MADV_DONTNEED)
  1227. obj_priv->dirty = 0;
  1228. for (i = 0; i < page_count; i++) {
  1229. if (obj_priv->dirty)
  1230. set_page_dirty(obj_priv->pages[i]);
  1231. if (obj_priv->madv == I915_MADV_WILLNEED)
  1232. mark_page_accessed(obj_priv->pages[i]);
  1233. page_cache_release(obj_priv->pages[i]);
  1234. }
  1235. obj_priv->dirty = 0;
  1236. drm_free_large(obj_priv->pages);
  1237. obj_priv->pages = NULL;
  1238. }
  1239. static void
  1240. i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
  1241. struct intel_ring_buffer *ring)
  1242. {
  1243. struct drm_device *dev = obj->dev;
  1244. drm_i915_private_t *dev_priv = dev->dev_private;
  1245. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1246. BUG_ON(ring == NULL);
  1247. obj_priv->ring = ring;
  1248. /* Add a reference if we're newly entering the active list. */
  1249. if (!obj_priv->active) {
  1250. drm_gem_object_reference(obj);
  1251. obj_priv->active = 1;
  1252. }
  1253. /* Move from whatever list we were on to the tail of execution. */
  1254. spin_lock(&dev_priv->mm.active_list_lock);
  1255. list_move_tail(&obj_priv->list, &ring->active_list);
  1256. spin_unlock(&dev_priv->mm.active_list_lock);
  1257. obj_priv->last_rendering_seqno = seqno;
  1258. }
  1259. static void
  1260. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  1261. {
  1262. struct drm_device *dev = obj->dev;
  1263. drm_i915_private_t *dev_priv = dev->dev_private;
  1264. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1265. BUG_ON(!obj_priv->active);
  1266. list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
  1267. obj_priv->last_rendering_seqno = 0;
  1268. }
  1269. /* Immediately discard the backing storage */
  1270. static void
  1271. i915_gem_object_truncate(struct drm_gem_object *obj)
  1272. {
  1273. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1274. struct inode *inode;
  1275. inode = obj->filp->f_path.dentry->d_inode;
  1276. if (inode->i_op->truncate)
  1277. inode->i_op->truncate (inode);
  1278. obj_priv->madv = __I915_MADV_PURGED;
  1279. }
  1280. static inline int
  1281. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
  1282. {
  1283. return obj_priv->madv == I915_MADV_DONTNEED;
  1284. }
  1285. static void
  1286. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  1287. {
  1288. struct drm_device *dev = obj->dev;
  1289. drm_i915_private_t *dev_priv = dev->dev_private;
  1290. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1291. i915_verify_inactive(dev, __FILE__, __LINE__);
  1292. if (obj_priv->pin_count != 0)
  1293. list_del_init(&obj_priv->list);
  1294. else
  1295. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1296. BUG_ON(!list_empty(&obj_priv->gpu_write_list));
  1297. obj_priv->last_rendering_seqno = 0;
  1298. obj_priv->ring = NULL;
  1299. if (obj_priv->active) {
  1300. obj_priv->active = 0;
  1301. drm_gem_object_unreference(obj);
  1302. }
  1303. i915_verify_inactive(dev, __FILE__, __LINE__);
  1304. }
  1305. static void
  1306. i915_gem_process_flushing_list(struct drm_device *dev,
  1307. uint32_t flush_domains, uint32_t seqno,
  1308. struct intel_ring_buffer *ring)
  1309. {
  1310. drm_i915_private_t *dev_priv = dev->dev_private;
  1311. struct drm_i915_gem_object *obj_priv, *next;
  1312. list_for_each_entry_safe(obj_priv, next,
  1313. &dev_priv->mm.gpu_write_list,
  1314. gpu_write_list) {
  1315. struct drm_gem_object *obj = &obj_priv->base;
  1316. if ((obj->write_domain & flush_domains) ==
  1317. obj->write_domain &&
  1318. obj_priv->ring->ring_flag == ring->ring_flag) {
  1319. uint32_t old_write_domain = obj->write_domain;
  1320. obj->write_domain = 0;
  1321. list_del_init(&obj_priv->gpu_write_list);
  1322. i915_gem_object_move_to_active(obj, seqno, ring);
  1323. /* update the fence lru list */
  1324. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  1325. struct drm_i915_fence_reg *reg =
  1326. &dev_priv->fence_regs[obj_priv->fence_reg];
  1327. list_move_tail(&reg->lru_list,
  1328. &dev_priv->mm.fence_list);
  1329. }
  1330. trace_i915_gem_object_change_domain(obj,
  1331. obj->read_domains,
  1332. old_write_domain);
  1333. }
  1334. }
  1335. }
  1336. uint32_t
  1337. i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
  1338. uint32_t flush_domains, struct intel_ring_buffer *ring)
  1339. {
  1340. drm_i915_private_t *dev_priv = dev->dev_private;
  1341. struct drm_i915_file_private *i915_file_priv = NULL;
  1342. struct drm_i915_gem_request *request;
  1343. uint32_t seqno;
  1344. int was_empty;
  1345. if (file_priv != NULL)
  1346. i915_file_priv = file_priv->driver_priv;
  1347. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1348. if (request == NULL)
  1349. return 0;
  1350. seqno = ring->add_request(dev, ring, file_priv, flush_domains);
  1351. request->seqno = seqno;
  1352. request->ring = ring;
  1353. request->emitted_jiffies = jiffies;
  1354. was_empty = list_empty(&ring->request_list);
  1355. list_add_tail(&request->list, &ring->request_list);
  1356. if (i915_file_priv) {
  1357. list_add_tail(&request->client_list,
  1358. &i915_file_priv->mm.request_list);
  1359. } else {
  1360. INIT_LIST_HEAD(&request->client_list);
  1361. }
  1362. /* Associate any objects on the flushing list matching the write
  1363. * domain we're flushing with our flush.
  1364. */
  1365. if (flush_domains != 0)
  1366. i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
  1367. if (!dev_priv->mm.suspended) {
  1368. mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
  1369. if (was_empty)
  1370. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1371. }
  1372. return seqno;
  1373. }
  1374. /**
  1375. * Command execution barrier
  1376. *
  1377. * Ensures that all commands in the ring are finished
  1378. * before signalling the CPU
  1379. */
  1380. static uint32_t
  1381. i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
  1382. {
  1383. uint32_t flush_domains = 0;
  1384. /* The sampler always gets flushed on i965 (sigh) */
  1385. if (IS_I965G(dev))
  1386. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  1387. ring->flush(dev, ring,
  1388. I915_GEM_DOMAIN_COMMAND, flush_domains);
  1389. return flush_domains;
  1390. }
  1391. /**
  1392. * Moves buffers associated only with the given active seqno from the active
  1393. * to inactive list, potentially freeing them.
  1394. */
  1395. static void
  1396. i915_gem_retire_request(struct drm_device *dev,
  1397. struct drm_i915_gem_request *request)
  1398. {
  1399. drm_i915_private_t *dev_priv = dev->dev_private;
  1400. trace_i915_gem_request_retire(dev, request->seqno);
  1401. /* Move any buffers on the active list that are no longer referenced
  1402. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1403. */
  1404. spin_lock(&dev_priv->mm.active_list_lock);
  1405. while (!list_empty(&request->ring->active_list)) {
  1406. struct drm_gem_object *obj;
  1407. struct drm_i915_gem_object *obj_priv;
  1408. obj_priv = list_first_entry(&request->ring->active_list,
  1409. struct drm_i915_gem_object,
  1410. list);
  1411. obj = &obj_priv->base;
  1412. /* If the seqno being retired doesn't match the oldest in the
  1413. * list, then the oldest in the list must still be newer than
  1414. * this seqno.
  1415. */
  1416. if (obj_priv->last_rendering_seqno != request->seqno)
  1417. goto out;
  1418. #if WATCH_LRU
  1419. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  1420. __func__, request->seqno, obj);
  1421. #endif
  1422. if (obj->write_domain != 0)
  1423. i915_gem_object_move_to_flushing(obj);
  1424. else {
  1425. /* Take a reference on the object so it won't be
  1426. * freed while the spinlock is held. The list
  1427. * protection for this spinlock is safe when breaking
  1428. * the lock like this since the next thing we do
  1429. * is just get the head of the list again.
  1430. */
  1431. drm_gem_object_reference(obj);
  1432. i915_gem_object_move_to_inactive(obj);
  1433. spin_unlock(&dev_priv->mm.active_list_lock);
  1434. drm_gem_object_unreference(obj);
  1435. spin_lock(&dev_priv->mm.active_list_lock);
  1436. }
  1437. }
  1438. out:
  1439. spin_unlock(&dev_priv->mm.active_list_lock);
  1440. }
  1441. /**
  1442. * Returns true if seq1 is later than seq2.
  1443. */
  1444. bool
  1445. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  1446. {
  1447. return (int32_t)(seq1 - seq2) >= 0;
  1448. }
  1449. uint32_t
  1450. i915_get_gem_seqno(struct drm_device *dev,
  1451. struct intel_ring_buffer *ring)
  1452. {
  1453. return ring->get_gem_seqno(dev, ring);
  1454. }
  1455. /**
  1456. * This function clears the request list as sequence numbers are passed.
  1457. */
  1458. static void
  1459. i915_gem_retire_requests_ring(struct drm_device *dev,
  1460. struct intel_ring_buffer *ring)
  1461. {
  1462. drm_i915_private_t *dev_priv = dev->dev_private;
  1463. uint32_t seqno;
  1464. if (!ring->status_page.page_addr
  1465. || list_empty(&ring->request_list))
  1466. return;
  1467. seqno = i915_get_gem_seqno(dev, ring);
  1468. while (!list_empty(&ring->request_list)) {
  1469. struct drm_i915_gem_request *request;
  1470. uint32_t retiring_seqno;
  1471. request = list_first_entry(&ring->request_list,
  1472. struct drm_i915_gem_request,
  1473. list);
  1474. retiring_seqno = request->seqno;
  1475. if (i915_seqno_passed(seqno, retiring_seqno) ||
  1476. atomic_read(&dev_priv->mm.wedged)) {
  1477. i915_gem_retire_request(dev, request);
  1478. list_del(&request->list);
  1479. list_del(&request->client_list);
  1480. kfree(request);
  1481. } else
  1482. break;
  1483. }
  1484. if (unlikely (dev_priv->trace_irq_seqno &&
  1485. i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
  1486. ring->user_irq_put(dev, ring);
  1487. dev_priv->trace_irq_seqno = 0;
  1488. }
  1489. }
  1490. void
  1491. i915_gem_retire_requests(struct drm_device *dev)
  1492. {
  1493. drm_i915_private_t *dev_priv = dev->dev_private;
  1494. if (!list_empty(&dev_priv->mm.deferred_free_list)) {
  1495. struct drm_i915_gem_object *obj_priv, *tmp;
  1496. /* We must be careful that during unbind() we do not
  1497. * accidentally infinitely recurse into retire requests.
  1498. * Currently:
  1499. * retire -> free -> unbind -> wait -> retire_ring
  1500. */
  1501. list_for_each_entry_safe(obj_priv, tmp,
  1502. &dev_priv->mm.deferred_free_list,
  1503. list)
  1504. i915_gem_free_object_tail(&obj_priv->base);
  1505. }
  1506. i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
  1507. if (HAS_BSD(dev))
  1508. i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
  1509. }
  1510. void
  1511. i915_gem_retire_work_handler(struct work_struct *work)
  1512. {
  1513. drm_i915_private_t *dev_priv;
  1514. struct drm_device *dev;
  1515. dev_priv = container_of(work, drm_i915_private_t,
  1516. mm.retire_work.work);
  1517. dev = dev_priv->dev;
  1518. mutex_lock(&dev->struct_mutex);
  1519. i915_gem_retire_requests(dev);
  1520. if (!dev_priv->mm.suspended &&
  1521. (!list_empty(&dev_priv->render_ring.request_list) ||
  1522. (HAS_BSD(dev) &&
  1523. !list_empty(&dev_priv->bsd_ring.request_list))))
  1524. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1525. mutex_unlock(&dev->struct_mutex);
  1526. }
  1527. int
  1528. i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
  1529. int interruptible, struct intel_ring_buffer *ring)
  1530. {
  1531. drm_i915_private_t *dev_priv = dev->dev_private;
  1532. u32 ier;
  1533. int ret = 0;
  1534. BUG_ON(seqno == 0);
  1535. if (atomic_read(&dev_priv->mm.wedged))
  1536. return -EIO;
  1537. if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
  1538. if (HAS_PCH_SPLIT(dev))
  1539. ier = I915_READ(DEIER) | I915_READ(GTIER);
  1540. else
  1541. ier = I915_READ(IER);
  1542. if (!ier) {
  1543. DRM_ERROR("something (likely vbetool) disabled "
  1544. "interrupts, re-enabling\n");
  1545. i915_driver_irq_preinstall(dev);
  1546. i915_driver_irq_postinstall(dev);
  1547. }
  1548. trace_i915_gem_request_wait_begin(dev, seqno);
  1549. ring->waiting_gem_seqno = seqno;
  1550. ring->user_irq_get(dev, ring);
  1551. if (interruptible)
  1552. ret = wait_event_interruptible(ring->irq_queue,
  1553. i915_seqno_passed(
  1554. ring->get_gem_seqno(dev, ring), seqno)
  1555. || atomic_read(&dev_priv->mm.wedged));
  1556. else
  1557. wait_event(ring->irq_queue,
  1558. i915_seqno_passed(
  1559. ring->get_gem_seqno(dev, ring), seqno)
  1560. || atomic_read(&dev_priv->mm.wedged));
  1561. ring->user_irq_put(dev, ring);
  1562. ring->waiting_gem_seqno = 0;
  1563. trace_i915_gem_request_wait_end(dev, seqno);
  1564. }
  1565. if (atomic_read(&dev_priv->mm.wedged))
  1566. ret = -EIO;
  1567. if (ret && ret != -ERESTARTSYS)
  1568. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  1569. __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
  1570. /* Directly dispatch request retiring. While we have the work queue
  1571. * to handle this, the waiter on a request often wants an associated
  1572. * buffer to have made it to the inactive list, and we would need
  1573. * a separate wait queue to handle that.
  1574. */
  1575. if (ret == 0)
  1576. i915_gem_retire_requests_ring(dev, ring);
  1577. return ret;
  1578. }
  1579. /**
  1580. * Waits for a sequence number to be signaled, and cleans up the
  1581. * request and object lists appropriately for that event.
  1582. */
  1583. static int
  1584. i915_wait_request(struct drm_device *dev, uint32_t seqno,
  1585. struct intel_ring_buffer *ring)
  1586. {
  1587. return i915_do_wait_request(dev, seqno, 1, ring);
  1588. }
  1589. static void
  1590. i915_gem_flush(struct drm_device *dev,
  1591. uint32_t invalidate_domains,
  1592. uint32_t flush_domains)
  1593. {
  1594. drm_i915_private_t *dev_priv = dev->dev_private;
  1595. if (flush_domains & I915_GEM_DOMAIN_CPU)
  1596. drm_agp_chipset_flush(dev);
  1597. dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
  1598. invalidate_domains,
  1599. flush_domains);
  1600. if (HAS_BSD(dev))
  1601. dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
  1602. invalidate_domains,
  1603. flush_domains);
  1604. }
  1605. /**
  1606. * Ensures that all rendering to the object has completed and the object is
  1607. * safe to unbind from the GTT or access from the CPU.
  1608. */
  1609. static int
  1610. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  1611. {
  1612. struct drm_device *dev = obj->dev;
  1613. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1614. int ret;
  1615. /* This function only exists to support waiting for existing rendering,
  1616. * not for emitting required flushes.
  1617. */
  1618. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1619. /* If there is rendering queued on the buffer being evicted, wait for
  1620. * it.
  1621. */
  1622. if (obj_priv->active) {
  1623. #if WATCH_BUF
  1624. DRM_INFO("%s: object %p wait for seqno %08x\n",
  1625. __func__, obj, obj_priv->last_rendering_seqno);
  1626. #endif
  1627. ret = i915_wait_request(dev,
  1628. obj_priv->last_rendering_seqno, obj_priv->ring);
  1629. if (ret != 0)
  1630. return ret;
  1631. }
  1632. return 0;
  1633. }
  1634. /**
  1635. * Unbinds an object from the GTT aperture.
  1636. */
  1637. int
  1638. i915_gem_object_unbind(struct drm_gem_object *obj)
  1639. {
  1640. struct drm_device *dev = obj->dev;
  1641. drm_i915_private_t *dev_priv = dev->dev_private;
  1642. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1643. int ret = 0;
  1644. #if WATCH_BUF
  1645. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  1646. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  1647. #endif
  1648. if (obj_priv->gtt_space == NULL)
  1649. return 0;
  1650. if (obj_priv->pin_count != 0) {
  1651. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1652. return -EINVAL;
  1653. }
  1654. /* blow away mappings if mapped through GTT */
  1655. i915_gem_release_mmap(obj);
  1656. /* Move the object to the CPU domain to ensure that
  1657. * any possible CPU writes while it's not in the GTT
  1658. * are flushed when we go to remap it. This will
  1659. * also ensure that all pending GPU writes are finished
  1660. * before we unbind.
  1661. */
  1662. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1663. if (ret == -ERESTARTSYS)
  1664. return ret;
  1665. /* Continue on if we fail due to EIO, the GPU is hung so we
  1666. * should be safe and we need to cleanup or else we might
  1667. * cause memory corruption through use-after-free.
  1668. */
  1669. BUG_ON(obj_priv->active);
  1670. /* release the fence reg _after_ flushing */
  1671. if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
  1672. i915_gem_clear_fence_reg(obj);
  1673. if (obj_priv->agp_mem != NULL) {
  1674. drm_unbind_agp(obj_priv->agp_mem);
  1675. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  1676. obj_priv->agp_mem = NULL;
  1677. }
  1678. i915_gem_object_put_pages(obj);
  1679. BUG_ON(obj_priv->pages_refcount);
  1680. if (obj_priv->gtt_space) {
  1681. atomic_dec(&dev->gtt_count);
  1682. atomic_sub(obj->size, &dev->gtt_memory);
  1683. drm_mm_put_block(obj_priv->gtt_space);
  1684. obj_priv->gtt_space = NULL;
  1685. }
  1686. /* Remove ourselves from the LRU list if present. */
  1687. spin_lock(&dev_priv->mm.active_list_lock);
  1688. if (!list_empty(&obj_priv->list))
  1689. list_del_init(&obj_priv->list);
  1690. spin_unlock(&dev_priv->mm.active_list_lock);
  1691. if (i915_gem_object_is_purgeable(obj_priv))
  1692. i915_gem_object_truncate(obj);
  1693. trace_i915_gem_object_unbind(obj);
  1694. return ret;
  1695. }
  1696. int
  1697. i915_gpu_idle(struct drm_device *dev)
  1698. {
  1699. drm_i915_private_t *dev_priv = dev->dev_private;
  1700. bool lists_empty;
  1701. uint32_t seqno1, seqno2;
  1702. int ret;
  1703. spin_lock(&dev_priv->mm.active_list_lock);
  1704. lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
  1705. list_empty(&dev_priv->render_ring.active_list) &&
  1706. (!HAS_BSD(dev) ||
  1707. list_empty(&dev_priv->bsd_ring.active_list)));
  1708. spin_unlock(&dev_priv->mm.active_list_lock);
  1709. if (lists_empty)
  1710. return 0;
  1711. /* Flush everything onto the inactive list. */
  1712. i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1713. seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
  1714. &dev_priv->render_ring);
  1715. if (seqno1 == 0)
  1716. return -ENOMEM;
  1717. ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
  1718. if (HAS_BSD(dev)) {
  1719. seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
  1720. &dev_priv->bsd_ring);
  1721. if (seqno2 == 0)
  1722. return -ENOMEM;
  1723. ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
  1724. if (ret)
  1725. return ret;
  1726. }
  1727. return ret;
  1728. }
  1729. int
  1730. i915_gem_object_get_pages(struct drm_gem_object *obj,
  1731. gfp_t gfpmask)
  1732. {
  1733. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1734. int page_count, i;
  1735. struct address_space *mapping;
  1736. struct inode *inode;
  1737. struct page *page;
  1738. BUG_ON(obj_priv->pages_refcount
  1739. == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
  1740. if (obj_priv->pages_refcount++ != 0)
  1741. return 0;
  1742. /* Get the list of pages out of our struct file. They'll be pinned
  1743. * at this point until we release them.
  1744. */
  1745. page_count = obj->size / PAGE_SIZE;
  1746. BUG_ON(obj_priv->pages != NULL);
  1747. obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
  1748. if (obj_priv->pages == NULL) {
  1749. obj_priv->pages_refcount--;
  1750. return -ENOMEM;
  1751. }
  1752. inode = obj->filp->f_path.dentry->d_inode;
  1753. mapping = inode->i_mapping;
  1754. for (i = 0; i < page_count; i++) {
  1755. page = read_cache_page_gfp(mapping, i,
  1756. GFP_HIGHUSER |
  1757. __GFP_COLD |
  1758. __GFP_RECLAIMABLE |
  1759. gfpmask);
  1760. if (IS_ERR(page))
  1761. goto err_pages;
  1762. obj_priv->pages[i] = page;
  1763. }
  1764. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1765. i915_gem_object_do_bit_17_swizzle(obj);
  1766. return 0;
  1767. err_pages:
  1768. while (i--)
  1769. page_cache_release(obj_priv->pages[i]);
  1770. drm_free_large(obj_priv->pages);
  1771. obj_priv->pages = NULL;
  1772. obj_priv->pages_refcount--;
  1773. return PTR_ERR(page);
  1774. }
  1775. static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
  1776. {
  1777. struct drm_gem_object *obj = reg->obj;
  1778. struct drm_device *dev = obj->dev;
  1779. drm_i915_private_t *dev_priv = dev->dev_private;
  1780. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1781. int regnum = obj_priv->fence_reg;
  1782. uint64_t val;
  1783. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1784. 0xfffff000) << 32;
  1785. val |= obj_priv->gtt_offset & 0xfffff000;
  1786. val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
  1787. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1788. if (obj_priv->tiling_mode == I915_TILING_Y)
  1789. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1790. val |= I965_FENCE_REG_VALID;
  1791. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
  1792. }
  1793. static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
  1794. {
  1795. struct drm_gem_object *obj = reg->obj;
  1796. struct drm_device *dev = obj->dev;
  1797. drm_i915_private_t *dev_priv = dev->dev_private;
  1798. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1799. int regnum = obj_priv->fence_reg;
  1800. uint64_t val;
  1801. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1802. 0xfffff000) << 32;
  1803. val |= obj_priv->gtt_offset & 0xfffff000;
  1804. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1805. if (obj_priv->tiling_mode == I915_TILING_Y)
  1806. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1807. val |= I965_FENCE_REG_VALID;
  1808. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  1809. }
  1810. static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
  1811. {
  1812. struct drm_gem_object *obj = reg->obj;
  1813. struct drm_device *dev = obj->dev;
  1814. drm_i915_private_t *dev_priv = dev->dev_private;
  1815. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1816. int regnum = obj_priv->fence_reg;
  1817. int tile_width;
  1818. uint32_t fence_reg, val;
  1819. uint32_t pitch_val;
  1820. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1821. (obj_priv->gtt_offset & (obj->size - 1))) {
  1822. WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
  1823. __func__, obj_priv->gtt_offset, obj->size);
  1824. return;
  1825. }
  1826. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1827. HAS_128_BYTE_Y_TILING(dev))
  1828. tile_width = 128;
  1829. else
  1830. tile_width = 512;
  1831. /* Note: pitch better be a power of two tile widths */
  1832. pitch_val = obj_priv->stride / tile_width;
  1833. pitch_val = ffs(pitch_val) - 1;
  1834. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1835. HAS_128_BYTE_Y_TILING(dev))
  1836. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  1837. else
  1838. WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
  1839. val = obj_priv->gtt_offset;
  1840. if (obj_priv->tiling_mode == I915_TILING_Y)
  1841. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1842. val |= I915_FENCE_SIZE_BITS(obj->size);
  1843. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1844. val |= I830_FENCE_REG_VALID;
  1845. if (regnum < 8)
  1846. fence_reg = FENCE_REG_830_0 + (regnum * 4);
  1847. else
  1848. fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
  1849. I915_WRITE(fence_reg, val);
  1850. }
  1851. static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
  1852. {
  1853. struct drm_gem_object *obj = reg->obj;
  1854. struct drm_device *dev = obj->dev;
  1855. drm_i915_private_t *dev_priv = dev->dev_private;
  1856. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1857. int regnum = obj_priv->fence_reg;
  1858. uint32_t val;
  1859. uint32_t pitch_val;
  1860. uint32_t fence_size_bits;
  1861. if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
  1862. (obj_priv->gtt_offset & (obj->size - 1))) {
  1863. WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
  1864. __func__, obj_priv->gtt_offset);
  1865. return;
  1866. }
  1867. pitch_val = obj_priv->stride / 128;
  1868. pitch_val = ffs(pitch_val) - 1;
  1869. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  1870. val = obj_priv->gtt_offset;
  1871. if (obj_priv->tiling_mode == I915_TILING_Y)
  1872. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1873. fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
  1874. WARN_ON(fence_size_bits & ~0x00000f00);
  1875. val |= fence_size_bits;
  1876. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1877. val |= I830_FENCE_REG_VALID;
  1878. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  1879. }
  1880. static int i915_find_fence_reg(struct drm_device *dev)
  1881. {
  1882. struct drm_i915_fence_reg *reg = NULL;
  1883. struct drm_i915_gem_object *obj_priv = NULL;
  1884. struct drm_i915_private *dev_priv = dev->dev_private;
  1885. struct drm_gem_object *obj = NULL;
  1886. int i, avail, ret;
  1887. /* First try to find a free reg */
  1888. avail = 0;
  1889. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  1890. reg = &dev_priv->fence_regs[i];
  1891. if (!reg->obj)
  1892. return i;
  1893. obj_priv = to_intel_bo(reg->obj);
  1894. if (!obj_priv->pin_count)
  1895. avail++;
  1896. }
  1897. if (avail == 0)
  1898. return -ENOSPC;
  1899. /* None available, try to steal one or wait for a user to finish */
  1900. i = I915_FENCE_REG_NONE;
  1901. list_for_each_entry(reg, &dev_priv->mm.fence_list,
  1902. lru_list) {
  1903. obj = reg->obj;
  1904. obj_priv = to_intel_bo(obj);
  1905. if (obj_priv->pin_count)
  1906. continue;
  1907. /* found one! */
  1908. i = obj_priv->fence_reg;
  1909. break;
  1910. }
  1911. BUG_ON(i == I915_FENCE_REG_NONE);
  1912. /* We only have a reference on obj from the active list. put_fence_reg
  1913. * might drop that one, causing a use-after-free in it. So hold a
  1914. * private reference to obj like the other callers of put_fence_reg
  1915. * (set_tiling ioctl) do. */
  1916. drm_gem_object_reference(obj);
  1917. ret = i915_gem_object_put_fence_reg(obj);
  1918. drm_gem_object_unreference(obj);
  1919. if (ret != 0)
  1920. return ret;
  1921. return i;
  1922. }
  1923. /**
  1924. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  1925. * @obj: object to map through a fence reg
  1926. *
  1927. * When mapping objects through the GTT, userspace wants to be able to write
  1928. * to them without having to worry about swizzling if the object is tiled.
  1929. *
  1930. * This function walks the fence regs looking for a free one for @obj,
  1931. * stealing one if it can't find any.
  1932. *
  1933. * It then sets up the reg based on the object's properties: address, pitch
  1934. * and tiling format.
  1935. */
  1936. int
  1937. i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
  1938. {
  1939. struct drm_device *dev = obj->dev;
  1940. struct drm_i915_private *dev_priv = dev->dev_private;
  1941. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1942. struct drm_i915_fence_reg *reg = NULL;
  1943. int ret;
  1944. /* Just update our place in the LRU if our fence is getting used. */
  1945. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  1946. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  1947. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  1948. return 0;
  1949. }
  1950. switch (obj_priv->tiling_mode) {
  1951. case I915_TILING_NONE:
  1952. WARN(1, "allocating a fence for non-tiled object?\n");
  1953. break;
  1954. case I915_TILING_X:
  1955. if (!obj_priv->stride)
  1956. return -EINVAL;
  1957. WARN((obj_priv->stride & (512 - 1)),
  1958. "object 0x%08x is X tiled but has non-512B pitch\n",
  1959. obj_priv->gtt_offset);
  1960. break;
  1961. case I915_TILING_Y:
  1962. if (!obj_priv->stride)
  1963. return -EINVAL;
  1964. WARN((obj_priv->stride & (128 - 1)),
  1965. "object 0x%08x is Y tiled but has non-128B pitch\n",
  1966. obj_priv->gtt_offset);
  1967. break;
  1968. }
  1969. ret = i915_find_fence_reg(dev);
  1970. if (ret < 0)
  1971. return ret;
  1972. obj_priv->fence_reg = ret;
  1973. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  1974. list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  1975. reg->obj = obj;
  1976. if (IS_GEN6(dev))
  1977. sandybridge_write_fence_reg(reg);
  1978. else if (IS_I965G(dev))
  1979. i965_write_fence_reg(reg);
  1980. else if (IS_I9XX(dev))
  1981. i915_write_fence_reg(reg);
  1982. else
  1983. i830_write_fence_reg(reg);
  1984. trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
  1985. obj_priv->tiling_mode);
  1986. return 0;
  1987. }
  1988. /**
  1989. * i915_gem_clear_fence_reg - clear out fence register info
  1990. * @obj: object to clear
  1991. *
  1992. * Zeroes out the fence register itself and clears out the associated
  1993. * data structures in dev_priv and obj_priv.
  1994. */
  1995. static void
  1996. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  1997. {
  1998. struct drm_device *dev = obj->dev;
  1999. drm_i915_private_t *dev_priv = dev->dev_private;
  2000. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2001. struct drm_i915_fence_reg *reg =
  2002. &dev_priv->fence_regs[obj_priv->fence_reg];
  2003. if (IS_GEN6(dev)) {
  2004. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
  2005. (obj_priv->fence_reg * 8), 0);
  2006. } else if (IS_I965G(dev)) {
  2007. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  2008. } else {
  2009. uint32_t fence_reg;
  2010. if (obj_priv->fence_reg < 8)
  2011. fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
  2012. else
  2013. fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
  2014. 8) * 4;
  2015. I915_WRITE(fence_reg, 0);
  2016. }
  2017. reg->obj = NULL;
  2018. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2019. list_del_init(&reg->lru_list);
  2020. }
  2021. /**
  2022. * i915_gem_object_put_fence_reg - waits on outstanding fenced access
  2023. * to the buffer to finish, and then resets the fence register.
  2024. * @obj: tiled object holding a fence register.
  2025. *
  2026. * Zeroes out the fence register itself and clears out the associated
  2027. * data structures in dev_priv and obj_priv.
  2028. */
  2029. int
  2030. i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
  2031. {
  2032. struct drm_device *dev = obj->dev;
  2033. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2034. if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
  2035. return 0;
  2036. /* If we've changed tiling, GTT-mappings of the object
  2037. * need to re-fault to ensure that the correct fence register
  2038. * setup is in place.
  2039. */
  2040. i915_gem_release_mmap(obj);
  2041. /* On the i915, GPU access to tiled buffers is via a fence,
  2042. * therefore we must wait for any outstanding access to complete
  2043. * before clearing the fence.
  2044. */
  2045. if (!IS_I965G(dev)) {
  2046. int ret;
  2047. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2048. if (ret != 0)
  2049. return ret;
  2050. ret = i915_gem_object_wait_rendering(obj);
  2051. if (ret != 0)
  2052. return ret;
  2053. }
  2054. i915_gem_object_flush_gtt_write_domain(obj);
  2055. i915_gem_clear_fence_reg (obj);
  2056. return 0;
  2057. }
  2058. /**
  2059. * Finds free space in the GTT aperture and binds the object there.
  2060. */
  2061. static int
  2062. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  2063. {
  2064. struct drm_device *dev = obj->dev;
  2065. drm_i915_private_t *dev_priv = dev->dev_private;
  2066. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2067. struct drm_mm_node *free_space;
  2068. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2069. int ret;
  2070. if (obj_priv->madv != I915_MADV_WILLNEED) {
  2071. DRM_ERROR("Attempting to bind a purgeable object\n");
  2072. return -EINVAL;
  2073. }
  2074. if (alignment == 0)
  2075. alignment = i915_gem_get_gtt_alignment(obj);
  2076. if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
  2077. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2078. return -EINVAL;
  2079. }
  2080. /* If the object is bigger than the entire aperture, reject it early
  2081. * before evicting everything in a vain attempt to find space.
  2082. */
  2083. if (obj->size > dev->gtt_total) {
  2084. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2085. return -E2BIG;
  2086. }
  2087. search_free:
  2088. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2089. obj->size, alignment, 0);
  2090. if (free_space != NULL) {
  2091. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  2092. alignment);
  2093. if (obj_priv->gtt_space != NULL)
  2094. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  2095. }
  2096. if (obj_priv->gtt_space == NULL) {
  2097. /* If the gtt is empty and we're still having trouble
  2098. * fitting our object in, we're out of memory.
  2099. */
  2100. #if WATCH_LRU
  2101. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  2102. #endif
  2103. ret = i915_gem_evict_something(dev, obj->size, alignment);
  2104. if (ret)
  2105. return ret;
  2106. goto search_free;
  2107. }
  2108. #if WATCH_BUF
  2109. DRM_INFO("Binding object of size %zd at 0x%08x\n",
  2110. obj->size, obj_priv->gtt_offset);
  2111. #endif
  2112. ret = i915_gem_object_get_pages(obj, gfpmask);
  2113. if (ret) {
  2114. drm_mm_put_block(obj_priv->gtt_space);
  2115. obj_priv->gtt_space = NULL;
  2116. if (ret == -ENOMEM) {
  2117. /* first try to clear up some space from the GTT */
  2118. ret = i915_gem_evict_something(dev, obj->size,
  2119. alignment);
  2120. if (ret) {
  2121. /* now try to shrink everyone else */
  2122. if (gfpmask) {
  2123. gfpmask = 0;
  2124. goto search_free;
  2125. }
  2126. return ret;
  2127. }
  2128. goto search_free;
  2129. }
  2130. return ret;
  2131. }
  2132. /* Create an AGP memory structure pointing at our pages, and bind it
  2133. * into the GTT.
  2134. */
  2135. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  2136. obj_priv->pages,
  2137. obj->size >> PAGE_SHIFT,
  2138. obj_priv->gtt_offset,
  2139. obj_priv->agp_type);
  2140. if (obj_priv->agp_mem == NULL) {
  2141. i915_gem_object_put_pages(obj);
  2142. drm_mm_put_block(obj_priv->gtt_space);
  2143. obj_priv->gtt_space = NULL;
  2144. ret = i915_gem_evict_something(dev, obj->size, alignment);
  2145. if (ret)
  2146. return ret;
  2147. goto search_free;
  2148. }
  2149. atomic_inc(&dev->gtt_count);
  2150. atomic_add(obj->size, &dev->gtt_memory);
  2151. /* keep track of bounds object by adding it to the inactive list */
  2152. list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  2153. /* Assert that the object is not currently in any GPU domain. As it
  2154. * wasn't in the GTT, there shouldn't be any way it could have been in
  2155. * a GPU cache
  2156. */
  2157. BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
  2158. BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
  2159. trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
  2160. return 0;
  2161. }
  2162. void
  2163. i915_gem_clflush_object(struct drm_gem_object *obj)
  2164. {
  2165. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2166. /* If we don't have a page list set up, then we're not pinned
  2167. * to GPU, and we can ignore the cache flush because it'll happen
  2168. * again at bind time.
  2169. */
  2170. if (obj_priv->pages == NULL)
  2171. return;
  2172. trace_i915_gem_object_clflush(obj);
  2173. drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
  2174. }
  2175. /** Flushes any GPU write domain for the object if it's dirty. */
  2176. static int
  2177. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  2178. {
  2179. struct drm_device *dev = obj->dev;
  2180. uint32_t old_write_domain;
  2181. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2182. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2183. return 0;
  2184. /* Queue the GPU write cache flushing we need. */
  2185. old_write_domain = obj->write_domain;
  2186. i915_gem_flush(dev, 0, obj->write_domain);
  2187. if (i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring) == 0)
  2188. return -ENOMEM;
  2189. trace_i915_gem_object_change_domain(obj,
  2190. obj->read_domains,
  2191. old_write_domain);
  2192. return 0;
  2193. }
  2194. /** Flushes the GTT write domain for the object if it's dirty. */
  2195. static void
  2196. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  2197. {
  2198. uint32_t old_write_domain;
  2199. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  2200. return;
  2201. /* No actual flushing is required for the GTT write domain. Writes
  2202. * to it immediately go to main memory as far as we know, so there's
  2203. * no chipset flush. It also doesn't land in render cache.
  2204. */
  2205. old_write_domain = obj->write_domain;
  2206. obj->write_domain = 0;
  2207. trace_i915_gem_object_change_domain(obj,
  2208. obj->read_domains,
  2209. old_write_domain);
  2210. }
  2211. /** Flushes the CPU write domain for the object if it's dirty. */
  2212. static void
  2213. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  2214. {
  2215. struct drm_device *dev = obj->dev;
  2216. uint32_t old_write_domain;
  2217. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  2218. return;
  2219. i915_gem_clflush_object(obj);
  2220. drm_agp_chipset_flush(dev);
  2221. old_write_domain = obj->write_domain;
  2222. obj->write_domain = 0;
  2223. trace_i915_gem_object_change_domain(obj,
  2224. obj->read_domains,
  2225. old_write_domain);
  2226. }
  2227. int
  2228. i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
  2229. {
  2230. int ret = 0;
  2231. switch (obj->write_domain) {
  2232. case I915_GEM_DOMAIN_GTT:
  2233. i915_gem_object_flush_gtt_write_domain(obj);
  2234. break;
  2235. case I915_GEM_DOMAIN_CPU:
  2236. i915_gem_object_flush_cpu_write_domain(obj);
  2237. break;
  2238. default:
  2239. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2240. break;
  2241. }
  2242. return ret;
  2243. }
  2244. /**
  2245. * Moves a single object to the GTT read, and possibly write domain.
  2246. *
  2247. * This function returns when the move is complete, including waiting on
  2248. * flushes to occur.
  2249. */
  2250. int
  2251. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  2252. {
  2253. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2254. uint32_t old_write_domain, old_read_domains;
  2255. int ret;
  2256. /* Not valid to be called on unbound objects. */
  2257. if (obj_priv->gtt_space == NULL)
  2258. return -EINVAL;
  2259. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2260. if (ret != 0)
  2261. return ret;
  2262. /* Wait on any GPU rendering and flushing to occur. */
  2263. ret = i915_gem_object_wait_rendering(obj);
  2264. if (ret != 0)
  2265. return ret;
  2266. old_write_domain = obj->write_domain;
  2267. old_read_domains = obj->read_domains;
  2268. /* If we're writing through the GTT domain, then CPU and GPU caches
  2269. * will need to be invalidated at next use.
  2270. */
  2271. if (write)
  2272. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  2273. i915_gem_object_flush_cpu_write_domain(obj);
  2274. /* It should now be out of any other write domains, and we can update
  2275. * the domain values for our changes.
  2276. */
  2277. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2278. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2279. if (write) {
  2280. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2281. obj_priv->dirty = 1;
  2282. }
  2283. trace_i915_gem_object_change_domain(obj,
  2284. old_read_domains,
  2285. old_write_domain);
  2286. return 0;
  2287. }
  2288. /*
  2289. * Prepare buffer for display plane. Use uninterruptible for possible flush
  2290. * wait, as in modesetting process we're not supposed to be interrupted.
  2291. */
  2292. int
  2293. i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
  2294. {
  2295. struct drm_device *dev = obj->dev;
  2296. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2297. uint32_t old_write_domain, old_read_domains;
  2298. int ret;
  2299. /* Not valid to be called on unbound objects. */
  2300. if (obj_priv->gtt_space == NULL)
  2301. return -EINVAL;
  2302. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2303. if (ret)
  2304. return ret;
  2305. /* Wait on any GPU rendering and flushing to occur. */
  2306. if (obj_priv->active) {
  2307. #if WATCH_BUF
  2308. DRM_INFO("%s: object %p wait for seqno %08x\n",
  2309. __func__, obj, obj_priv->last_rendering_seqno);
  2310. #endif
  2311. ret = i915_do_wait_request(dev,
  2312. obj_priv->last_rendering_seqno,
  2313. 0,
  2314. obj_priv->ring);
  2315. if (ret != 0)
  2316. return ret;
  2317. }
  2318. i915_gem_object_flush_cpu_write_domain(obj);
  2319. old_write_domain = obj->write_domain;
  2320. old_read_domains = obj->read_domains;
  2321. /* It should now be out of any other write domains, and we can update
  2322. * the domain values for our changes.
  2323. */
  2324. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2325. obj->read_domains = I915_GEM_DOMAIN_GTT;
  2326. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2327. obj_priv->dirty = 1;
  2328. trace_i915_gem_object_change_domain(obj,
  2329. old_read_domains,
  2330. old_write_domain);
  2331. return 0;
  2332. }
  2333. /**
  2334. * Moves a single object to the CPU read, and possibly write domain.
  2335. *
  2336. * This function returns when the move is complete, including waiting on
  2337. * flushes to occur.
  2338. */
  2339. static int
  2340. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  2341. {
  2342. uint32_t old_write_domain, old_read_domains;
  2343. int ret;
  2344. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2345. if (ret)
  2346. return ret;
  2347. /* Wait on any GPU rendering and flushing to occur. */
  2348. ret = i915_gem_object_wait_rendering(obj);
  2349. if (ret != 0)
  2350. return ret;
  2351. i915_gem_object_flush_gtt_write_domain(obj);
  2352. /* If we have a partially-valid cache of the object in the CPU,
  2353. * finish invalidating it and free the per-page flags.
  2354. */
  2355. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2356. old_write_domain = obj->write_domain;
  2357. old_read_domains = obj->read_domains;
  2358. /* Flush the CPU cache if it's still invalid. */
  2359. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2360. i915_gem_clflush_object(obj);
  2361. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2362. }
  2363. /* It should now be out of any other write domains, and we can update
  2364. * the domain values for our changes.
  2365. */
  2366. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2367. /* If we're writing through the CPU, then the GPU read domains will
  2368. * need to be invalidated at next use.
  2369. */
  2370. if (write) {
  2371. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  2372. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2373. }
  2374. trace_i915_gem_object_change_domain(obj,
  2375. old_read_domains,
  2376. old_write_domain);
  2377. return 0;
  2378. }
  2379. /*
  2380. * Set the next domain for the specified object. This
  2381. * may not actually perform the necessary flushing/invaliding though,
  2382. * as that may want to be batched with other set_domain operations
  2383. *
  2384. * This is (we hope) the only really tricky part of gem. The goal
  2385. * is fairly simple -- track which caches hold bits of the object
  2386. * and make sure they remain coherent. A few concrete examples may
  2387. * help to explain how it works. For shorthand, we use the notation
  2388. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  2389. * a pair of read and write domain masks.
  2390. *
  2391. * Case 1: the batch buffer
  2392. *
  2393. * 1. Allocated
  2394. * 2. Written by CPU
  2395. * 3. Mapped to GTT
  2396. * 4. Read by GPU
  2397. * 5. Unmapped from GTT
  2398. * 6. Freed
  2399. *
  2400. * Let's take these a step at a time
  2401. *
  2402. * 1. Allocated
  2403. * Pages allocated from the kernel may still have
  2404. * cache contents, so we set them to (CPU, CPU) always.
  2405. * 2. Written by CPU (using pwrite)
  2406. * The pwrite function calls set_domain (CPU, CPU) and
  2407. * this function does nothing (as nothing changes)
  2408. * 3. Mapped by GTT
  2409. * This function asserts that the object is not
  2410. * currently in any GPU-based read or write domains
  2411. * 4. Read by GPU
  2412. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  2413. * As write_domain is zero, this function adds in the
  2414. * current read domains (CPU+COMMAND, 0).
  2415. * flush_domains is set to CPU.
  2416. * invalidate_domains is set to COMMAND
  2417. * clflush is run to get data out of the CPU caches
  2418. * then i915_dev_set_domain calls i915_gem_flush to
  2419. * emit an MI_FLUSH and drm_agp_chipset_flush
  2420. * 5. Unmapped from GTT
  2421. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  2422. * flush_domains and invalidate_domains end up both zero
  2423. * so no flushing/invalidating happens
  2424. * 6. Freed
  2425. * yay, done
  2426. *
  2427. * Case 2: The shared render buffer
  2428. *
  2429. * 1. Allocated
  2430. * 2. Mapped to GTT
  2431. * 3. Read/written by GPU
  2432. * 4. set_domain to (CPU,CPU)
  2433. * 5. Read/written by CPU
  2434. * 6. Read/written by GPU
  2435. *
  2436. * 1. Allocated
  2437. * Same as last example, (CPU, CPU)
  2438. * 2. Mapped to GTT
  2439. * Nothing changes (assertions find that it is not in the GPU)
  2440. * 3. Read/written by GPU
  2441. * execbuffer calls set_domain (RENDER, RENDER)
  2442. * flush_domains gets CPU
  2443. * invalidate_domains gets GPU
  2444. * clflush (obj)
  2445. * MI_FLUSH and drm_agp_chipset_flush
  2446. * 4. set_domain (CPU, CPU)
  2447. * flush_domains gets GPU
  2448. * invalidate_domains gets CPU
  2449. * wait_rendering (obj) to make sure all drawing is complete.
  2450. * This will include an MI_FLUSH to get the data from GPU
  2451. * to memory
  2452. * clflush (obj) to invalidate the CPU cache
  2453. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  2454. * 5. Read/written by CPU
  2455. * cache lines are loaded and dirtied
  2456. * 6. Read written by GPU
  2457. * Same as last GPU access
  2458. *
  2459. * Case 3: The constant buffer
  2460. *
  2461. * 1. Allocated
  2462. * 2. Written by CPU
  2463. * 3. Read by GPU
  2464. * 4. Updated (written) by CPU again
  2465. * 5. Read by GPU
  2466. *
  2467. * 1. Allocated
  2468. * (CPU, CPU)
  2469. * 2. Written by CPU
  2470. * (CPU, CPU)
  2471. * 3. Read by GPU
  2472. * (CPU+RENDER, 0)
  2473. * flush_domains = CPU
  2474. * invalidate_domains = RENDER
  2475. * clflush (obj)
  2476. * MI_FLUSH
  2477. * drm_agp_chipset_flush
  2478. * 4. Updated (written) by CPU again
  2479. * (CPU, CPU)
  2480. * flush_domains = 0 (no previous write domain)
  2481. * invalidate_domains = 0 (no new read domains)
  2482. * 5. Read by GPU
  2483. * (CPU+RENDER, 0)
  2484. * flush_domains = CPU
  2485. * invalidate_domains = RENDER
  2486. * clflush (obj)
  2487. * MI_FLUSH
  2488. * drm_agp_chipset_flush
  2489. */
  2490. static void
  2491. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
  2492. {
  2493. struct drm_device *dev = obj->dev;
  2494. drm_i915_private_t *dev_priv = dev->dev_private;
  2495. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2496. uint32_t invalidate_domains = 0;
  2497. uint32_t flush_domains = 0;
  2498. uint32_t old_read_domains;
  2499. BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
  2500. BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
  2501. intel_mark_busy(dev, obj);
  2502. #if WATCH_BUF
  2503. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  2504. __func__, obj,
  2505. obj->read_domains, obj->pending_read_domains,
  2506. obj->write_domain, obj->pending_write_domain);
  2507. #endif
  2508. /*
  2509. * If the object isn't moving to a new write domain,
  2510. * let the object stay in multiple read domains
  2511. */
  2512. if (obj->pending_write_domain == 0)
  2513. obj->pending_read_domains |= obj->read_domains;
  2514. else
  2515. obj_priv->dirty = 1;
  2516. /*
  2517. * Flush the current write domain if
  2518. * the new read domains don't match. Invalidate
  2519. * any read domains which differ from the old
  2520. * write domain
  2521. */
  2522. if (obj->write_domain &&
  2523. obj->write_domain != obj->pending_read_domains) {
  2524. flush_domains |= obj->write_domain;
  2525. invalidate_domains |=
  2526. obj->pending_read_domains & ~obj->write_domain;
  2527. }
  2528. /*
  2529. * Invalidate any read caches which may have
  2530. * stale data. That is, any new read domains.
  2531. */
  2532. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  2533. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  2534. #if WATCH_BUF
  2535. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  2536. __func__, flush_domains, invalidate_domains);
  2537. #endif
  2538. i915_gem_clflush_object(obj);
  2539. }
  2540. old_read_domains = obj->read_domains;
  2541. /* The actual obj->write_domain will be updated with
  2542. * pending_write_domain after we emit the accumulated flush for all
  2543. * of our domain changes in execbuffers (which clears objects'
  2544. * write_domains). So if we have a current write domain that we
  2545. * aren't changing, set pending_write_domain to that.
  2546. */
  2547. if (flush_domains == 0 && obj->pending_write_domain == 0)
  2548. obj->pending_write_domain = obj->write_domain;
  2549. obj->read_domains = obj->pending_read_domains;
  2550. if (flush_domains & I915_GEM_GPU_DOMAINS) {
  2551. if (obj_priv->ring == &dev_priv->render_ring)
  2552. dev_priv->flush_rings |= FLUSH_RENDER_RING;
  2553. else if (obj_priv->ring == &dev_priv->bsd_ring)
  2554. dev_priv->flush_rings |= FLUSH_BSD_RING;
  2555. }
  2556. dev->invalidate_domains |= invalidate_domains;
  2557. dev->flush_domains |= flush_domains;
  2558. #if WATCH_BUF
  2559. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  2560. __func__,
  2561. obj->read_domains, obj->write_domain,
  2562. dev->invalidate_domains, dev->flush_domains);
  2563. #endif
  2564. trace_i915_gem_object_change_domain(obj,
  2565. old_read_domains,
  2566. obj->write_domain);
  2567. }
  2568. /**
  2569. * Moves the object from a partially CPU read to a full one.
  2570. *
  2571. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2572. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2573. */
  2574. static void
  2575. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  2576. {
  2577. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2578. if (!obj_priv->page_cpu_valid)
  2579. return;
  2580. /* If we're partially in the CPU read domain, finish moving it in.
  2581. */
  2582. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  2583. int i;
  2584. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  2585. if (obj_priv->page_cpu_valid[i])
  2586. continue;
  2587. drm_clflush_pages(obj_priv->pages + i, 1);
  2588. }
  2589. }
  2590. /* Free the page_cpu_valid mappings which are now stale, whether
  2591. * or not we've got I915_GEM_DOMAIN_CPU.
  2592. */
  2593. kfree(obj_priv->page_cpu_valid);
  2594. obj_priv->page_cpu_valid = NULL;
  2595. }
  2596. /**
  2597. * Set the CPU read domain on a range of the object.
  2598. *
  2599. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2600. * not entirely valid. The page_cpu_valid member of the object flags which
  2601. * pages have been flushed, and will be respected by
  2602. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2603. * of the whole object.
  2604. *
  2605. * This function returns when the move is complete, including waiting on
  2606. * flushes to occur.
  2607. */
  2608. static int
  2609. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  2610. uint64_t offset, uint64_t size)
  2611. {
  2612. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2613. uint32_t old_read_domains;
  2614. int i, ret;
  2615. if (offset == 0 && size == obj->size)
  2616. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2617. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2618. if (ret)
  2619. return ret;
  2620. /* Wait on any GPU rendering and flushing to occur. */
  2621. ret = i915_gem_object_wait_rendering(obj);
  2622. if (ret != 0)
  2623. return ret;
  2624. i915_gem_object_flush_gtt_write_domain(obj);
  2625. /* If we're already fully in the CPU read domain, we're done. */
  2626. if (obj_priv->page_cpu_valid == NULL &&
  2627. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2628. return 0;
  2629. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2630. * newly adding I915_GEM_DOMAIN_CPU
  2631. */
  2632. if (obj_priv->page_cpu_valid == NULL) {
  2633. obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
  2634. GFP_KERNEL);
  2635. if (obj_priv->page_cpu_valid == NULL)
  2636. return -ENOMEM;
  2637. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2638. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  2639. /* Flush the cache on any pages that are still invalid from the CPU's
  2640. * perspective.
  2641. */
  2642. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2643. i++) {
  2644. if (obj_priv->page_cpu_valid[i])
  2645. continue;
  2646. drm_clflush_pages(obj_priv->pages + i, 1);
  2647. obj_priv->page_cpu_valid[i] = 1;
  2648. }
  2649. /* It should now be out of any other write domains, and we can update
  2650. * the domain values for our changes.
  2651. */
  2652. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2653. old_read_domains = obj->read_domains;
  2654. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2655. trace_i915_gem_object_change_domain(obj,
  2656. old_read_domains,
  2657. obj->write_domain);
  2658. return 0;
  2659. }
  2660. /**
  2661. * Pin an object to the GTT and evaluate the relocations landing in it.
  2662. */
  2663. static int
  2664. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  2665. struct drm_file *file_priv,
  2666. struct drm_i915_gem_exec_object2 *entry,
  2667. struct drm_i915_gem_relocation_entry *relocs)
  2668. {
  2669. struct drm_device *dev = obj->dev;
  2670. drm_i915_private_t *dev_priv = dev->dev_private;
  2671. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2672. int i, ret;
  2673. void __iomem *reloc_page;
  2674. bool need_fence;
  2675. need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  2676. obj_priv->tiling_mode != I915_TILING_NONE;
  2677. /* Check fence reg constraints and rebind if necessary */
  2678. if (need_fence &&
  2679. !i915_gem_object_fence_offset_ok(obj,
  2680. obj_priv->tiling_mode)) {
  2681. ret = i915_gem_object_unbind(obj);
  2682. if (ret)
  2683. return ret;
  2684. }
  2685. /* Choose the GTT offset for our buffer and put it there. */
  2686. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  2687. if (ret)
  2688. return ret;
  2689. /*
  2690. * Pre-965 chips need a fence register set up in order to
  2691. * properly handle blits to/from tiled surfaces.
  2692. */
  2693. if (need_fence) {
  2694. ret = i915_gem_object_get_fence_reg(obj);
  2695. if (ret != 0) {
  2696. i915_gem_object_unpin(obj);
  2697. return ret;
  2698. }
  2699. }
  2700. entry->offset = obj_priv->gtt_offset;
  2701. /* Apply the relocations, using the GTT aperture to avoid cache
  2702. * flushing requirements.
  2703. */
  2704. for (i = 0; i < entry->relocation_count; i++) {
  2705. struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
  2706. struct drm_gem_object *target_obj;
  2707. struct drm_i915_gem_object *target_obj_priv;
  2708. uint32_t reloc_val, reloc_offset;
  2709. uint32_t __iomem *reloc_entry;
  2710. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  2711. reloc->target_handle);
  2712. if (target_obj == NULL) {
  2713. i915_gem_object_unpin(obj);
  2714. return -EBADF;
  2715. }
  2716. target_obj_priv = to_intel_bo(target_obj);
  2717. #if WATCH_RELOC
  2718. DRM_INFO("%s: obj %p offset %08x target %d "
  2719. "read %08x write %08x gtt %08x "
  2720. "presumed %08x delta %08x\n",
  2721. __func__,
  2722. obj,
  2723. (int) reloc->offset,
  2724. (int) reloc->target_handle,
  2725. (int) reloc->read_domains,
  2726. (int) reloc->write_domain,
  2727. (int) target_obj_priv->gtt_offset,
  2728. (int) reloc->presumed_offset,
  2729. reloc->delta);
  2730. #endif
  2731. /* The target buffer should have appeared before us in the
  2732. * exec_object list, so it should have a GTT space bound by now.
  2733. */
  2734. if (target_obj_priv->gtt_space == NULL) {
  2735. DRM_ERROR("No GTT space found for object %d\n",
  2736. reloc->target_handle);
  2737. drm_gem_object_unreference(target_obj);
  2738. i915_gem_object_unpin(obj);
  2739. return -EINVAL;
  2740. }
  2741. /* Validate that the target is in a valid r/w GPU domain */
  2742. if (reloc->write_domain & (reloc->write_domain - 1)) {
  2743. DRM_ERROR("reloc with multiple write domains: "
  2744. "obj %p target %d offset %d "
  2745. "read %08x write %08x",
  2746. obj, reloc->target_handle,
  2747. (int) reloc->offset,
  2748. reloc->read_domains,
  2749. reloc->write_domain);
  2750. return -EINVAL;
  2751. }
  2752. if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
  2753. reloc->read_domains & I915_GEM_DOMAIN_CPU) {
  2754. DRM_ERROR("reloc with read/write CPU domains: "
  2755. "obj %p target %d offset %d "
  2756. "read %08x write %08x",
  2757. obj, reloc->target_handle,
  2758. (int) reloc->offset,
  2759. reloc->read_domains,
  2760. reloc->write_domain);
  2761. drm_gem_object_unreference(target_obj);
  2762. i915_gem_object_unpin(obj);
  2763. return -EINVAL;
  2764. }
  2765. if (reloc->write_domain && target_obj->pending_write_domain &&
  2766. reloc->write_domain != target_obj->pending_write_domain) {
  2767. DRM_ERROR("Write domain conflict: "
  2768. "obj %p target %d offset %d "
  2769. "new %08x old %08x\n",
  2770. obj, reloc->target_handle,
  2771. (int) reloc->offset,
  2772. reloc->write_domain,
  2773. target_obj->pending_write_domain);
  2774. drm_gem_object_unreference(target_obj);
  2775. i915_gem_object_unpin(obj);
  2776. return -EINVAL;
  2777. }
  2778. target_obj->pending_read_domains |= reloc->read_domains;
  2779. target_obj->pending_write_domain |= reloc->write_domain;
  2780. /* If the relocation already has the right value in it, no
  2781. * more work needs to be done.
  2782. */
  2783. if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
  2784. drm_gem_object_unreference(target_obj);
  2785. continue;
  2786. }
  2787. /* Check that the relocation address is valid... */
  2788. if (reloc->offset > obj->size - 4) {
  2789. DRM_ERROR("Relocation beyond object bounds: "
  2790. "obj %p target %d offset %d size %d.\n",
  2791. obj, reloc->target_handle,
  2792. (int) reloc->offset, (int) obj->size);
  2793. drm_gem_object_unreference(target_obj);
  2794. i915_gem_object_unpin(obj);
  2795. return -EINVAL;
  2796. }
  2797. if (reloc->offset & 3) {
  2798. DRM_ERROR("Relocation not 4-byte aligned: "
  2799. "obj %p target %d offset %d.\n",
  2800. obj, reloc->target_handle,
  2801. (int) reloc->offset);
  2802. drm_gem_object_unreference(target_obj);
  2803. i915_gem_object_unpin(obj);
  2804. return -EINVAL;
  2805. }
  2806. /* and points to somewhere within the target object. */
  2807. if (reloc->delta >= target_obj->size) {
  2808. DRM_ERROR("Relocation beyond target object bounds: "
  2809. "obj %p target %d delta %d size %d.\n",
  2810. obj, reloc->target_handle,
  2811. (int) reloc->delta, (int) target_obj->size);
  2812. drm_gem_object_unreference(target_obj);
  2813. i915_gem_object_unpin(obj);
  2814. return -EINVAL;
  2815. }
  2816. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  2817. if (ret != 0) {
  2818. drm_gem_object_unreference(target_obj);
  2819. i915_gem_object_unpin(obj);
  2820. return -EINVAL;
  2821. }
  2822. /* Map the page containing the relocation we're going to
  2823. * perform.
  2824. */
  2825. reloc_offset = obj_priv->gtt_offset + reloc->offset;
  2826. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2827. (reloc_offset &
  2828. ~(PAGE_SIZE - 1)),
  2829. KM_USER0);
  2830. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2831. (reloc_offset & (PAGE_SIZE - 1)));
  2832. reloc_val = target_obj_priv->gtt_offset + reloc->delta;
  2833. #if WATCH_BUF
  2834. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2835. obj, (unsigned int) reloc->offset,
  2836. readl(reloc_entry), reloc_val);
  2837. #endif
  2838. writel(reloc_val, reloc_entry);
  2839. io_mapping_unmap_atomic(reloc_page, KM_USER0);
  2840. /* The updated presumed offset for this entry will be
  2841. * copied back out to the user.
  2842. */
  2843. reloc->presumed_offset = target_obj_priv->gtt_offset;
  2844. drm_gem_object_unreference(target_obj);
  2845. }
  2846. #if WATCH_BUF
  2847. if (0)
  2848. i915_gem_dump_object(obj, 128, __func__, ~0);
  2849. #endif
  2850. return 0;
  2851. }
  2852. /* Throttle our rendering by waiting until the ring has completed our requests
  2853. * emitted over 20 msec ago.
  2854. *
  2855. * Note that if we were to use the current jiffies each time around the loop,
  2856. * we wouldn't escape the function with any frames outstanding if the time to
  2857. * render a frame was over 20ms.
  2858. *
  2859. * This should get us reasonable parallelism between CPU and GPU but also
  2860. * relatively low latency when blocking on a particular request to finish.
  2861. */
  2862. static int
  2863. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2864. {
  2865. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2866. int ret = 0;
  2867. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2868. mutex_lock(&dev->struct_mutex);
  2869. while (!list_empty(&i915_file_priv->mm.request_list)) {
  2870. struct drm_i915_gem_request *request;
  2871. request = list_first_entry(&i915_file_priv->mm.request_list,
  2872. struct drm_i915_gem_request,
  2873. client_list);
  2874. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2875. break;
  2876. ret = i915_wait_request(dev, request->seqno, request->ring);
  2877. if (ret != 0)
  2878. break;
  2879. }
  2880. mutex_unlock(&dev->struct_mutex);
  2881. return ret;
  2882. }
  2883. static int
  2884. i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
  2885. uint32_t buffer_count,
  2886. struct drm_i915_gem_relocation_entry **relocs)
  2887. {
  2888. uint32_t reloc_count = 0, reloc_index = 0, i;
  2889. int ret;
  2890. *relocs = NULL;
  2891. for (i = 0; i < buffer_count; i++) {
  2892. if (reloc_count + exec_list[i].relocation_count < reloc_count)
  2893. return -EINVAL;
  2894. reloc_count += exec_list[i].relocation_count;
  2895. }
  2896. *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
  2897. if (*relocs == NULL) {
  2898. DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
  2899. return -ENOMEM;
  2900. }
  2901. for (i = 0; i < buffer_count; i++) {
  2902. struct drm_i915_gem_relocation_entry __user *user_relocs;
  2903. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  2904. ret = copy_from_user(&(*relocs)[reloc_index],
  2905. user_relocs,
  2906. exec_list[i].relocation_count *
  2907. sizeof(**relocs));
  2908. if (ret != 0) {
  2909. drm_free_large(*relocs);
  2910. *relocs = NULL;
  2911. return -EFAULT;
  2912. }
  2913. reloc_index += exec_list[i].relocation_count;
  2914. }
  2915. return 0;
  2916. }
  2917. static int
  2918. i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
  2919. uint32_t buffer_count,
  2920. struct drm_i915_gem_relocation_entry *relocs)
  2921. {
  2922. uint32_t reloc_count = 0, i;
  2923. int ret = 0;
  2924. if (relocs == NULL)
  2925. return 0;
  2926. for (i = 0; i < buffer_count; i++) {
  2927. struct drm_i915_gem_relocation_entry __user *user_relocs;
  2928. int unwritten;
  2929. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  2930. unwritten = copy_to_user(user_relocs,
  2931. &relocs[reloc_count],
  2932. exec_list[i].relocation_count *
  2933. sizeof(*relocs));
  2934. if (unwritten) {
  2935. ret = -EFAULT;
  2936. goto err;
  2937. }
  2938. reloc_count += exec_list[i].relocation_count;
  2939. }
  2940. err:
  2941. drm_free_large(relocs);
  2942. return ret;
  2943. }
  2944. static int
  2945. i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
  2946. uint64_t exec_offset)
  2947. {
  2948. uint32_t exec_start, exec_len;
  2949. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  2950. exec_len = (uint32_t) exec->batch_len;
  2951. if ((exec_start | exec_len) & 0x7)
  2952. return -EINVAL;
  2953. if (!exec_start)
  2954. return -EINVAL;
  2955. return 0;
  2956. }
  2957. static int
  2958. i915_gem_wait_for_pending_flip(struct drm_device *dev,
  2959. struct drm_gem_object **object_list,
  2960. int count)
  2961. {
  2962. drm_i915_private_t *dev_priv = dev->dev_private;
  2963. struct drm_i915_gem_object *obj_priv;
  2964. DEFINE_WAIT(wait);
  2965. int i, ret = 0;
  2966. for (;;) {
  2967. prepare_to_wait(&dev_priv->pending_flip_queue,
  2968. &wait, TASK_INTERRUPTIBLE);
  2969. for (i = 0; i < count; i++) {
  2970. obj_priv = to_intel_bo(object_list[i]);
  2971. if (atomic_read(&obj_priv->pending_flip) > 0)
  2972. break;
  2973. }
  2974. if (i == count)
  2975. break;
  2976. if (!signal_pending(current)) {
  2977. mutex_unlock(&dev->struct_mutex);
  2978. schedule();
  2979. mutex_lock(&dev->struct_mutex);
  2980. continue;
  2981. }
  2982. ret = -ERESTARTSYS;
  2983. break;
  2984. }
  2985. finish_wait(&dev_priv->pending_flip_queue, &wait);
  2986. return ret;
  2987. }
  2988. int
  2989. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  2990. struct drm_file *file_priv,
  2991. struct drm_i915_gem_execbuffer2 *args,
  2992. struct drm_i915_gem_exec_object2 *exec_list)
  2993. {
  2994. drm_i915_private_t *dev_priv = dev->dev_private;
  2995. struct drm_gem_object **object_list = NULL;
  2996. struct drm_gem_object *batch_obj;
  2997. struct drm_i915_gem_object *obj_priv;
  2998. struct drm_clip_rect *cliprects = NULL;
  2999. struct drm_i915_gem_relocation_entry *relocs = NULL;
  3000. int ret = 0, ret2, i, pinned = 0;
  3001. uint64_t exec_offset;
  3002. uint32_t seqno, flush_domains, reloc_index;
  3003. int pin_tries, flips;
  3004. struct intel_ring_buffer *ring = NULL;
  3005. #if WATCH_EXEC
  3006. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3007. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3008. #endif
  3009. if (args->flags & I915_EXEC_BSD) {
  3010. if (!HAS_BSD(dev)) {
  3011. DRM_ERROR("execbuf with wrong flag\n");
  3012. return -EINVAL;
  3013. }
  3014. ring = &dev_priv->bsd_ring;
  3015. } else {
  3016. ring = &dev_priv->render_ring;
  3017. }
  3018. if (args->buffer_count < 1) {
  3019. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3020. return -EINVAL;
  3021. }
  3022. object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
  3023. if (object_list == NULL) {
  3024. DRM_ERROR("Failed to allocate object list for %d buffers\n",
  3025. args->buffer_count);
  3026. ret = -ENOMEM;
  3027. goto pre_mutex_err;
  3028. }
  3029. if (args->num_cliprects != 0) {
  3030. cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
  3031. GFP_KERNEL);
  3032. if (cliprects == NULL) {
  3033. ret = -ENOMEM;
  3034. goto pre_mutex_err;
  3035. }
  3036. ret = copy_from_user(cliprects,
  3037. (struct drm_clip_rect __user *)
  3038. (uintptr_t) args->cliprects_ptr,
  3039. sizeof(*cliprects) * args->num_cliprects);
  3040. if (ret != 0) {
  3041. DRM_ERROR("copy %d cliprects failed: %d\n",
  3042. args->num_cliprects, ret);
  3043. goto pre_mutex_err;
  3044. }
  3045. }
  3046. ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
  3047. &relocs);
  3048. if (ret != 0)
  3049. goto pre_mutex_err;
  3050. mutex_lock(&dev->struct_mutex);
  3051. i915_verify_inactive(dev, __FILE__, __LINE__);
  3052. if (atomic_read(&dev_priv->mm.wedged)) {
  3053. mutex_unlock(&dev->struct_mutex);
  3054. ret = -EIO;
  3055. goto pre_mutex_err;
  3056. }
  3057. if (dev_priv->mm.suspended) {
  3058. mutex_unlock(&dev->struct_mutex);
  3059. ret = -EBUSY;
  3060. goto pre_mutex_err;
  3061. }
  3062. /* Look up object handles */
  3063. flips = 0;
  3064. for (i = 0; i < args->buffer_count; i++) {
  3065. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  3066. exec_list[i].handle);
  3067. if (object_list[i] == NULL) {
  3068. DRM_ERROR("Invalid object handle %d at index %d\n",
  3069. exec_list[i].handle, i);
  3070. /* prevent error path from reading uninitialized data */
  3071. args->buffer_count = i + 1;
  3072. ret = -EBADF;
  3073. goto err;
  3074. }
  3075. obj_priv = to_intel_bo(object_list[i]);
  3076. if (obj_priv->in_execbuffer) {
  3077. DRM_ERROR("Object %p appears more than once in object list\n",
  3078. object_list[i]);
  3079. /* prevent error path from reading uninitialized data */
  3080. args->buffer_count = i + 1;
  3081. ret = -EBADF;
  3082. goto err;
  3083. }
  3084. obj_priv->in_execbuffer = true;
  3085. flips += atomic_read(&obj_priv->pending_flip);
  3086. }
  3087. if (flips > 0) {
  3088. ret = i915_gem_wait_for_pending_flip(dev, object_list,
  3089. args->buffer_count);
  3090. if (ret)
  3091. goto err;
  3092. }
  3093. /* Pin and relocate */
  3094. for (pin_tries = 0; ; pin_tries++) {
  3095. ret = 0;
  3096. reloc_index = 0;
  3097. for (i = 0; i < args->buffer_count; i++) {
  3098. object_list[i]->pending_read_domains = 0;
  3099. object_list[i]->pending_write_domain = 0;
  3100. ret = i915_gem_object_pin_and_relocate(object_list[i],
  3101. file_priv,
  3102. &exec_list[i],
  3103. &relocs[reloc_index]);
  3104. if (ret)
  3105. break;
  3106. pinned = i + 1;
  3107. reloc_index += exec_list[i].relocation_count;
  3108. }
  3109. /* success */
  3110. if (ret == 0)
  3111. break;
  3112. /* error other than GTT full, or we've already tried again */
  3113. if (ret != -ENOSPC || pin_tries >= 1) {
  3114. if (ret != -ERESTARTSYS) {
  3115. unsigned long long total_size = 0;
  3116. int num_fences = 0;
  3117. for (i = 0; i < args->buffer_count; i++) {
  3118. obj_priv = to_intel_bo(object_list[i]);
  3119. total_size += object_list[i]->size;
  3120. num_fences +=
  3121. exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
  3122. obj_priv->tiling_mode != I915_TILING_NONE;
  3123. }
  3124. DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
  3125. pinned+1, args->buffer_count,
  3126. total_size, num_fences,
  3127. ret);
  3128. DRM_ERROR("%d objects [%d pinned], "
  3129. "%d object bytes [%d pinned], "
  3130. "%d/%d gtt bytes\n",
  3131. atomic_read(&dev->object_count),
  3132. atomic_read(&dev->pin_count),
  3133. atomic_read(&dev->object_memory),
  3134. atomic_read(&dev->pin_memory),
  3135. atomic_read(&dev->gtt_memory),
  3136. dev->gtt_total);
  3137. }
  3138. goto err;
  3139. }
  3140. /* unpin all of our buffers */
  3141. for (i = 0; i < pinned; i++)
  3142. i915_gem_object_unpin(object_list[i]);
  3143. pinned = 0;
  3144. /* evict everyone we can from the aperture */
  3145. ret = i915_gem_evict_everything(dev);
  3146. if (ret && ret != -ENOSPC)
  3147. goto err;
  3148. }
  3149. /* Set the pending read domains for the batch buffer to COMMAND */
  3150. batch_obj = object_list[args->buffer_count-1];
  3151. if (batch_obj->pending_write_domain) {
  3152. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  3153. ret = -EINVAL;
  3154. goto err;
  3155. }
  3156. batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  3157. /* Sanity check the batch buffer, prior to moving objects */
  3158. exec_offset = exec_list[args->buffer_count - 1].offset;
  3159. ret = i915_gem_check_execbuffer (args, exec_offset);
  3160. if (ret != 0) {
  3161. DRM_ERROR("execbuf with invalid offset/length\n");
  3162. goto err;
  3163. }
  3164. i915_verify_inactive(dev, __FILE__, __LINE__);
  3165. /* Zero the global flush/invalidate flags. These
  3166. * will be modified as new domains are computed
  3167. * for each object
  3168. */
  3169. dev->invalidate_domains = 0;
  3170. dev->flush_domains = 0;
  3171. dev_priv->flush_rings = 0;
  3172. for (i = 0; i < args->buffer_count; i++) {
  3173. struct drm_gem_object *obj = object_list[i];
  3174. /* Compute new gpu domains and update invalidate/flush */
  3175. i915_gem_object_set_to_gpu_domain(obj);
  3176. }
  3177. i915_verify_inactive(dev, __FILE__, __LINE__);
  3178. if (dev->invalidate_domains | dev->flush_domains) {
  3179. #if WATCH_EXEC
  3180. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  3181. __func__,
  3182. dev->invalidate_domains,
  3183. dev->flush_domains);
  3184. #endif
  3185. i915_gem_flush(dev,
  3186. dev->invalidate_domains,
  3187. dev->flush_domains);
  3188. if (dev_priv->flush_rings & FLUSH_RENDER_RING)
  3189. (void)i915_add_request(dev, file_priv,
  3190. dev->flush_domains,
  3191. &dev_priv->render_ring);
  3192. if (dev_priv->flush_rings & FLUSH_BSD_RING)
  3193. (void)i915_add_request(dev, file_priv,
  3194. dev->flush_domains,
  3195. &dev_priv->bsd_ring);
  3196. }
  3197. for (i = 0; i < args->buffer_count; i++) {
  3198. struct drm_gem_object *obj = object_list[i];
  3199. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3200. uint32_t old_write_domain = obj->write_domain;
  3201. obj->write_domain = obj->pending_write_domain;
  3202. if (obj->write_domain)
  3203. list_move_tail(&obj_priv->gpu_write_list,
  3204. &dev_priv->mm.gpu_write_list);
  3205. else
  3206. list_del_init(&obj_priv->gpu_write_list);
  3207. trace_i915_gem_object_change_domain(obj,
  3208. obj->read_domains,
  3209. old_write_domain);
  3210. }
  3211. i915_verify_inactive(dev, __FILE__, __LINE__);
  3212. #if WATCH_COHERENCY
  3213. for (i = 0; i < args->buffer_count; i++) {
  3214. i915_gem_object_check_coherency(object_list[i],
  3215. exec_list[i].handle);
  3216. }
  3217. #endif
  3218. #if WATCH_EXEC
  3219. i915_gem_dump_object(batch_obj,
  3220. args->batch_len,
  3221. __func__,
  3222. ~0);
  3223. #endif
  3224. /* Exec the batchbuffer */
  3225. ret = ring->dispatch_gem_execbuffer(dev, ring, args,
  3226. cliprects, exec_offset);
  3227. if (ret) {
  3228. DRM_ERROR("dispatch failed %d\n", ret);
  3229. goto err;
  3230. }
  3231. /*
  3232. * Ensure that the commands in the batch buffer are
  3233. * finished before the interrupt fires
  3234. */
  3235. flush_domains = i915_retire_commands(dev, ring);
  3236. i915_verify_inactive(dev, __FILE__, __LINE__);
  3237. /*
  3238. * Get a seqno representing the execution of the current buffer,
  3239. * which we can wait on. We would like to mitigate these interrupts,
  3240. * likely by only creating seqnos occasionally (so that we have
  3241. * *some* interrupts representing completion of buffers that we can
  3242. * wait on when trying to clear up gtt space).
  3243. */
  3244. seqno = i915_add_request(dev, file_priv, flush_domains, ring);
  3245. BUG_ON(seqno == 0);
  3246. for (i = 0; i < args->buffer_count; i++) {
  3247. struct drm_gem_object *obj = object_list[i];
  3248. obj_priv = to_intel_bo(obj);
  3249. i915_gem_object_move_to_active(obj, seqno, ring);
  3250. #if WATCH_LRU
  3251. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  3252. #endif
  3253. }
  3254. #if WATCH_LRU
  3255. i915_dump_lru(dev, __func__);
  3256. #endif
  3257. i915_verify_inactive(dev, __FILE__, __LINE__);
  3258. err:
  3259. for (i = 0; i < pinned; i++)
  3260. i915_gem_object_unpin(object_list[i]);
  3261. for (i = 0; i < args->buffer_count; i++) {
  3262. if (object_list[i]) {
  3263. obj_priv = to_intel_bo(object_list[i]);
  3264. obj_priv->in_execbuffer = false;
  3265. }
  3266. drm_gem_object_unreference(object_list[i]);
  3267. }
  3268. mutex_unlock(&dev->struct_mutex);
  3269. pre_mutex_err:
  3270. /* Copy the updated relocations out regardless of current error
  3271. * state. Failure to update the relocs would mean that the next
  3272. * time userland calls execbuf, it would do so with presumed offset
  3273. * state that didn't match the actual object state.
  3274. */
  3275. ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
  3276. relocs);
  3277. if (ret2 != 0) {
  3278. DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
  3279. if (ret == 0)
  3280. ret = ret2;
  3281. }
  3282. drm_free_large(object_list);
  3283. kfree(cliprects);
  3284. return ret;
  3285. }
  3286. /*
  3287. * Legacy execbuffer just creates an exec2 list from the original exec object
  3288. * list array and passes it to the real function.
  3289. */
  3290. int
  3291. i915_gem_execbuffer(struct drm_device *dev, void *data,
  3292. struct drm_file *file_priv)
  3293. {
  3294. struct drm_i915_gem_execbuffer *args = data;
  3295. struct drm_i915_gem_execbuffer2 exec2;
  3296. struct drm_i915_gem_exec_object *exec_list = NULL;
  3297. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3298. int ret, i;
  3299. #if WATCH_EXEC
  3300. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3301. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3302. #endif
  3303. if (args->buffer_count < 1) {
  3304. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3305. return -EINVAL;
  3306. }
  3307. /* Copy in the exec list from userland */
  3308. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  3309. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3310. if (exec_list == NULL || exec2_list == NULL) {
  3311. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3312. args->buffer_count);
  3313. drm_free_large(exec_list);
  3314. drm_free_large(exec2_list);
  3315. return -ENOMEM;
  3316. }
  3317. ret = copy_from_user(exec_list,
  3318. (struct drm_i915_relocation_entry __user *)
  3319. (uintptr_t) args->buffers_ptr,
  3320. sizeof(*exec_list) * args->buffer_count);
  3321. if (ret != 0) {
  3322. DRM_ERROR("copy %d exec entries failed %d\n",
  3323. args->buffer_count, ret);
  3324. drm_free_large(exec_list);
  3325. drm_free_large(exec2_list);
  3326. return -EFAULT;
  3327. }
  3328. for (i = 0; i < args->buffer_count; i++) {
  3329. exec2_list[i].handle = exec_list[i].handle;
  3330. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  3331. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  3332. exec2_list[i].alignment = exec_list[i].alignment;
  3333. exec2_list[i].offset = exec_list[i].offset;
  3334. if (!IS_I965G(dev))
  3335. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  3336. else
  3337. exec2_list[i].flags = 0;
  3338. }
  3339. exec2.buffers_ptr = args->buffers_ptr;
  3340. exec2.buffer_count = args->buffer_count;
  3341. exec2.batch_start_offset = args->batch_start_offset;
  3342. exec2.batch_len = args->batch_len;
  3343. exec2.DR1 = args->DR1;
  3344. exec2.DR4 = args->DR4;
  3345. exec2.num_cliprects = args->num_cliprects;
  3346. exec2.cliprects_ptr = args->cliprects_ptr;
  3347. exec2.flags = I915_EXEC_RENDER;
  3348. ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
  3349. if (!ret) {
  3350. /* Copy the new buffer offsets back to the user's exec list. */
  3351. for (i = 0; i < args->buffer_count; i++)
  3352. exec_list[i].offset = exec2_list[i].offset;
  3353. /* ... and back out to userspace */
  3354. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3355. (uintptr_t) args->buffers_ptr,
  3356. exec_list,
  3357. sizeof(*exec_list) * args->buffer_count);
  3358. if (ret) {
  3359. ret = -EFAULT;
  3360. DRM_ERROR("failed to copy %d exec entries "
  3361. "back to user (%d)\n",
  3362. args->buffer_count, ret);
  3363. }
  3364. }
  3365. drm_free_large(exec_list);
  3366. drm_free_large(exec2_list);
  3367. return ret;
  3368. }
  3369. int
  3370. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  3371. struct drm_file *file_priv)
  3372. {
  3373. struct drm_i915_gem_execbuffer2 *args = data;
  3374. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3375. int ret;
  3376. #if WATCH_EXEC
  3377. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3378. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3379. #endif
  3380. if (args->buffer_count < 1) {
  3381. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  3382. return -EINVAL;
  3383. }
  3384. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3385. if (exec2_list == NULL) {
  3386. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3387. args->buffer_count);
  3388. return -ENOMEM;
  3389. }
  3390. ret = copy_from_user(exec2_list,
  3391. (struct drm_i915_relocation_entry __user *)
  3392. (uintptr_t) args->buffers_ptr,
  3393. sizeof(*exec2_list) * args->buffer_count);
  3394. if (ret != 0) {
  3395. DRM_ERROR("copy %d exec entries failed %d\n",
  3396. args->buffer_count, ret);
  3397. drm_free_large(exec2_list);
  3398. return -EFAULT;
  3399. }
  3400. ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
  3401. if (!ret) {
  3402. /* Copy the new buffer offsets back to the user's exec list. */
  3403. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3404. (uintptr_t) args->buffers_ptr,
  3405. exec2_list,
  3406. sizeof(*exec2_list) * args->buffer_count);
  3407. if (ret) {
  3408. ret = -EFAULT;
  3409. DRM_ERROR("failed to copy %d exec entries "
  3410. "back to user (%d)\n",
  3411. args->buffer_count, ret);
  3412. }
  3413. }
  3414. drm_free_large(exec2_list);
  3415. return ret;
  3416. }
  3417. int
  3418. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  3419. {
  3420. struct drm_device *dev = obj->dev;
  3421. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3422. int ret;
  3423. BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  3424. i915_verify_inactive(dev, __FILE__, __LINE__);
  3425. if (obj_priv->gtt_space != NULL) {
  3426. if (alignment == 0)
  3427. alignment = i915_gem_get_gtt_alignment(obj);
  3428. if (obj_priv->gtt_offset & (alignment - 1)) {
  3429. WARN(obj_priv->pin_count,
  3430. "bo is already pinned with incorrect alignment:"
  3431. " offset=%x, req.alignment=%x\n",
  3432. obj_priv->gtt_offset, alignment);
  3433. ret = i915_gem_object_unbind(obj);
  3434. if (ret)
  3435. return ret;
  3436. }
  3437. }
  3438. if (obj_priv->gtt_space == NULL) {
  3439. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  3440. if (ret)
  3441. return ret;
  3442. }
  3443. obj_priv->pin_count++;
  3444. /* If the object is not active and not pending a flush,
  3445. * remove it from the inactive list
  3446. */
  3447. if (obj_priv->pin_count == 1) {
  3448. atomic_inc(&dev->pin_count);
  3449. atomic_add(obj->size, &dev->pin_memory);
  3450. if (!obj_priv->active &&
  3451. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  3452. list_del_init(&obj_priv->list);
  3453. }
  3454. i915_verify_inactive(dev, __FILE__, __LINE__);
  3455. return 0;
  3456. }
  3457. void
  3458. i915_gem_object_unpin(struct drm_gem_object *obj)
  3459. {
  3460. struct drm_device *dev = obj->dev;
  3461. drm_i915_private_t *dev_priv = dev->dev_private;
  3462. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3463. i915_verify_inactive(dev, __FILE__, __LINE__);
  3464. obj_priv->pin_count--;
  3465. BUG_ON(obj_priv->pin_count < 0);
  3466. BUG_ON(obj_priv->gtt_space == NULL);
  3467. /* If the object is no longer pinned, and is
  3468. * neither active nor being flushed, then stick it on
  3469. * the inactive list
  3470. */
  3471. if (obj_priv->pin_count == 0) {
  3472. if (!obj_priv->active &&
  3473. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  3474. list_move_tail(&obj_priv->list,
  3475. &dev_priv->mm.inactive_list);
  3476. atomic_dec(&dev->pin_count);
  3477. atomic_sub(obj->size, &dev->pin_memory);
  3478. }
  3479. i915_verify_inactive(dev, __FILE__, __LINE__);
  3480. }
  3481. int
  3482. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3483. struct drm_file *file_priv)
  3484. {
  3485. struct drm_i915_gem_pin *args = data;
  3486. struct drm_gem_object *obj;
  3487. struct drm_i915_gem_object *obj_priv;
  3488. int ret;
  3489. mutex_lock(&dev->struct_mutex);
  3490. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3491. if (obj == NULL) {
  3492. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  3493. args->handle);
  3494. mutex_unlock(&dev->struct_mutex);
  3495. return -EBADF;
  3496. }
  3497. obj_priv = to_intel_bo(obj);
  3498. if (obj_priv->madv != I915_MADV_WILLNEED) {
  3499. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3500. drm_gem_object_unreference(obj);
  3501. mutex_unlock(&dev->struct_mutex);
  3502. return -EINVAL;
  3503. }
  3504. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  3505. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3506. args->handle);
  3507. drm_gem_object_unreference(obj);
  3508. mutex_unlock(&dev->struct_mutex);
  3509. return -EINVAL;
  3510. }
  3511. obj_priv->user_pin_count++;
  3512. obj_priv->pin_filp = file_priv;
  3513. if (obj_priv->user_pin_count == 1) {
  3514. ret = i915_gem_object_pin(obj, args->alignment);
  3515. if (ret != 0) {
  3516. drm_gem_object_unreference(obj);
  3517. mutex_unlock(&dev->struct_mutex);
  3518. return ret;
  3519. }
  3520. }
  3521. /* XXX - flush the CPU caches for pinned objects
  3522. * as the X server doesn't manage domains yet
  3523. */
  3524. i915_gem_object_flush_cpu_write_domain(obj);
  3525. args->offset = obj_priv->gtt_offset;
  3526. drm_gem_object_unreference(obj);
  3527. mutex_unlock(&dev->struct_mutex);
  3528. return 0;
  3529. }
  3530. int
  3531. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3532. struct drm_file *file_priv)
  3533. {
  3534. struct drm_i915_gem_pin *args = data;
  3535. struct drm_gem_object *obj;
  3536. struct drm_i915_gem_object *obj_priv;
  3537. mutex_lock(&dev->struct_mutex);
  3538. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3539. if (obj == NULL) {
  3540. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  3541. args->handle);
  3542. mutex_unlock(&dev->struct_mutex);
  3543. return -EBADF;
  3544. }
  3545. obj_priv = to_intel_bo(obj);
  3546. if (obj_priv->pin_filp != file_priv) {
  3547. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3548. args->handle);
  3549. drm_gem_object_unreference(obj);
  3550. mutex_unlock(&dev->struct_mutex);
  3551. return -EINVAL;
  3552. }
  3553. obj_priv->user_pin_count--;
  3554. if (obj_priv->user_pin_count == 0) {
  3555. obj_priv->pin_filp = NULL;
  3556. i915_gem_object_unpin(obj);
  3557. }
  3558. drm_gem_object_unreference(obj);
  3559. mutex_unlock(&dev->struct_mutex);
  3560. return 0;
  3561. }
  3562. int
  3563. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3564. struct drm_file *file_priv)
  3565. {
  3566. struct drm_i915_gem_busy *args = data;
  3567. struct drm_gem_object *obj;
  3568. struct drm_i915_gem_object *obj_priv;
  3569. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3570. if (obj == NULL) {
  3571. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  3572. args->handle);
  3573. return -EBADF;
  3574. }
  3575. mutex_lock(&dev->struct_mutex);
  3576. /* Count all active objects as busy, even if they are currently not used
  3577. * by the gpu. Users of this interface expect objects to eventually
  3578. * become non-busy without any further actions, therefore emit any
  3579. * necessary flushes here.
  3580. */
  3581. obj_priv = to_intel_bo(obj);
  3582. args->busy = obj_priv->active;
  3583. if (args->busy) {
  3584. /* Unconditionally flush objects, even when the gpu still uses this
  3585. * object. Userspace calling this function indicates that it wants to
  3586. * use this buffer rather sooner than later, so issuing the required
  3587. * flush earlier is beneficial.
  3588. */
  3589. if (obj->write_domain) {
  3590. i915_gem_flush(dev, 0, obj->write_domain);
  3591. (void)i915_add_request(dev, file_priv, obj->write_domain, obj_priv->ring);
  3592. }
  3593. /* Update the active list for the hardware's current position.
  3594. * Otherwise this only updates on a delayed timer or when irqs
  3595. * are actually unmasked, and our working set ends up being
  3596. * larger than required.
  3597. */
  3598. i915_gem_retire_requests_ring(dev, obj_priv->ring);
  3599. args->busy = obj_priv->active;
  3600. }
  3601. drm_gem_object_unreference(obj);
  3602. mutex_unlock(&dev->struct_mutex);
  3603. return 0;
  3604. }
  3605. int
  3606. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3607. struct drm_file *file_priv)
  3608. {
  3609. return i915_gem_ring_throttle(dev, file_priv);
  3610. }
  3611. int
  3612. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3613. struct drm_file *file_priv)
  3614. {
  3615. struct drm_i915_gem_madvise *args = data;
  3616. struct drm_gem_object *obj;
  3617. struct drm_i915_gem_object *obj_priv;
  3618. switch (args->madv) {
  3619. case I915_MADV_DONTNEED:
  3620. case I915_MADV_WILLNEED:
  3621. break;
  3622. default:
  3623. return -EINVAL;
  3624. }
  3625. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3626. if (obj == NULL) {
  3627. DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
  3628. args->handle);
  3629. return -EBADF;
  3630. }
  3631. mutex_lock(&dev->struct_mutex);
  3632. obj_priv = to_intel_bo(obj);
  3633. if (obj_priv->pin_count) {
  3634. drm_gem_object_unreference(obj);
  3635. mutex_unlock(&dev->struct_mutex);
  3636. DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
  3637. return -EINVAL;
  3638. }
  3639. if (obj_priv->madv != __I915_MADV_PURGED)
  3640. obj_priv->madv = args->madv;
  3641. /* if the object is no longer bound, discard its backing storage */
  3642. if (i915_gem_object_is_purgeable(obj_priv) &&
  3643. obj_priv->gtt_space == NULL)
  3644. i915_gem_object_truncate(obj);
  3645. args->retained = obj_priv->madv != __I915_MADV_PURGED;
  3646. drm_gem_object_unreference(obj);
  3647. mutex_unlock(&dev->struct_mutex);
  3648. return 0;
  3649. }
  3650. struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
  3651. size_t size)
  3652. {
  3653. struct drm_i915_gem_object *obj;
  3654. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3655. if (obj == NULL)
  3656. return NULL;
  3657. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3658. kfree(obj);
  3659. return NULL;
  3660. }
  3661. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3662. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3663. obj->agp_type = AGP_USER_MEMORY;
  3664. obj->base.driver_private = NULL;
  3665. obj->fence_reg = I915_FENCE_REG_NONE;
  3666. INIT_LIST_HEAD(&obj->list);
  3667. INIT_LIST_HEAD(&obj->gpu_write_list);
  3668. obj->madv = I915_MADV_WILLNEED;
  3669. trace_i915_gem_object_create(&obj->base);
  3670. return &obj->base;
  3671. }
  3672. int i915_gem_init_object(struct drm_gem_object *obj)
  3673. {
  3674. BUG();
  3675. return 0;
  3676. }
  3677. static void i915_gem_free_object_tail(struct drm_gem_object *obj)
  3678. {
  3679. struct drm_device *dev = obj->dev;
  3680. drm_i915_private_t *dev_priv = dev->dev_private;
  3681. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3682. int ret;
  3683. ret = i915_gem_object_unbind(obj);
  3684. if (ret == -ERESTARTSYS) {
  3685. list_move(&obj_priv->list,
  3686. &dev_priv->mm.deferred_free_list);
  3687. return;
  3688. }
  3689. if (obj_priv->mmap_offset)
  3690. i915_gem_free_mmap_offset(obj);
  3691. drm_gem_object_release(obj);
  3692. kfree(obj_priv->page_cpu_valid);
  3693. kfree(obj_priv->bit_17);
  3694. kfree(obj_priv);
  3695. }
  3696. void i915_gem_free_object(struct drm_gem_object *obj)
  3697. {
  3698. struct drm_device *dev = obj->dev;
  3699. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3700. trace_i915_gem_object_destroy(obj);
  3701. while (obj_priv->pin_count > 0)
  3702. i915_gem_object_unpin(obj);
  3703. if (obj_priv->phys_obj)
  3704. i915_gem_detach_phys_object(dev, obj);
  3705. i915_gem_free_object_tail(obj);
  3706. }
  3707. int
  3708. i915_gem_idle(struct drm_device *dev)
  3709. {
  3710. drm_i915_private_t *dev_priv = dev->dev_private;
  3711. int ret;
  3712. mutex_lock(&dev->struct_mutex);
  3713. if (dev_priv->mm.suspended ||
  3714. (dev_priv->render_ring.gem_object == NULL) ||
  3715. (HAS_BSD(dev) &&
  3716. dev_priv->bsd_ring.gem_object == NULL)) {
  3717. mutex_unlock(&dev->struct_mutex);
  3718. return 0;
  3719. }
  3720. ret = i915_gpu_idle(dev);
  3721. if (ret) {
  3722. mutex_unlock(&dev->struct_mutex);
  3723. return ret;
  3724. }
  3725. /* Under UMS, be paranoid and evict. */
  3726. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  3727. ret = i915_gem_evict_inactive(dev);
  3728. if (ret) {
  3729. mutex_unlock(&dev->struct_mutex);
  3730. return ret;
  3731. }
  3732. }
  3733. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3734. * We need to replace this with a semaphore, or something.
  3735. * And not confound mm.suspended!
  3736. */
  3737. dev_priv->mm.suspended = 1;
  3738. del_timer(&dev_priv->hangcheck_timer);
  3739. i915_kernel_lost_context(dev);
  3740. i915_gem_cleanup_ringbuffer(dev);
  3741. mutex_unlock(&dev->struct_mutex);
  3742. /* Cancel the retire work handler, which should be idle now. */
  3743. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3744. return 0;
  3745. }
  3746. /*
  3747. * 965+ support PIPE_CONTROL commands, which provide finer grained control
  3748. * over cache flushing.
  3749. */
  3750. static int
  3751. i915_gem_init_pipe_control(struct drm_device *dev)
  3752. {
  3753. drm_i915_private_t *dev_priv = dev->dev_private;
  3754. struct drm_gem_object *obj;
  3755. struct drm_i915_gem_object *obj_priv;
  3756. int ret;
  3757. obj = i915_gem_alloc_object(dev, 4096);
  3758. if (obj == NULL) {
  3759. DRM_ERROR("Failed to allocate seqno page\n");
  3760. ret = -ENOMEM;
  3761. goto err;
  3762. }
  3763. obj_priv = to_intel_bo(obj);
  3764. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  3765. ret = i915_gem_object_pin(obj, 4096);
  3766. if (ret)
  3767. goto err_unref;
  3768. dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
  3769. dev_priv->seqno_page = kmap(obj_priv->pages[0]);
  3770. if (dev_priv->seqno_page == NULL)
  3771. goto err_unpin;
  3772. dev_priv->seqno_obj = obj;
  3773. memset(dev_priv->seqno_page, 0, PAGE_SIZE);
  3774. return 0;
  3775. err_unpin:
  3776. i915_gem_object_unpin(obj);
  3777. err_unref:
  3778. drm_gem_object_unreference(obj);
  3779. err:
  3780. return ret;
  3781. }
  3782. static void
  3783. i915_gem_cleanup_pipe_control(struct drm_device *dev)
  3784. {
  3785. drm_i915_private_t *dev_priv = dev->dev_private;
  3786. struct drm_gem_object *obj;
  3787. struct drm_i915_gem_object *obj_priv;
  3788. obj = dev_priv->seqno_obj;
  3789. obj_priv = to_intel_bo(obj);
  3790. kunmap(obj_priv->pages[0]);
  3791. i915_gem_object_unpin(obj);
  3792. drm_gem_object_unreference(obj);
  3793. dev_priv->seqno_obj = NULL;
  3794. dev_priv->seqno_page = NULL;
  3795. }
  3796. int
  3797. i915_gem_init_ringbuffer(struct drm_device *dev)
  3798. {
  3799. drm_i915_private_t *dev_priv = dev->dev_private;
  3800. int ret;
  3801. dev_priv->render_ring = render_ring;
  3802. if (!I915_NEED_GFX_HWS(dev)) {
  3803. dev_priv->render_ring.status_page.page_addr
  3804. = dev_priv->status_page_dmah->vaddr;
  3805. memset(dev_priv->render_ring.status_page.page_addr,
  3806. 0, PAGE_SIZE);
  3807. }
  3808. if (HAS_PIPE_CONTROL(dev)) {
  3809. ret = i915_gem_init_pipe_control(dev);
  3810. if (ret)
  3811. return ret;
  3812. }
  3813. ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
  3814. if (ret)
  3815. goto cleanup_pipe_control;
  3816. if (HAS_BSD(dev)) {
  3817. dev_priv->bsd_ring = bsd_ring;
  3818. ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
  3819. if (ret)
  3820. goto cleanup_render_ring;
  3821. }
  3822. dev_priv->next_seqno = 1;
  3823. return 0;
  3824. cleanup_render_ring:
  3825. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3826. cleanup_pipe_control:
  3827. if (HAS_PIPE_CONTROL(dev))
  3828. i915_gem_cleanup_pipe_control(dev);
  3829. return ret;
  3830. }
  3831. void
  3832. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3833. {
  3834. drm_i915_private_t *dev_priv = dev->dev_private;
  3835. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3836. if (HAS_BSD(dev))
  3837. intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
  3838. if (HAS_PIPE_CONTROL(dev))
  3839. i915_gem_cleanup_pipe_control(dev);
  3840. }
  3841. int
  3842. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3843. struct drm_file *file_priv)
  3844. {
  3845. drm_i915_private_t *dev_priv = dev->dev_private;
  3846. int ret;
  3847. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3848. return 0;
  3849. if (atomic_read(&dev_priv->mm.wedged)) {
  3850. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3851. atomic_set(&dev_priv->mm.wedged, 0);
  3852. }
  3853. mutex_lock(&dev->struct_mutex);
  3854. dev_priv->mm.suspended = 0;
  3855. ret = i915_gem_init_ringbuffer(dev);
  3856. if (ret != 0) {
  3857. mutex_unlock(&dev->struct_mutex);
  3858. return ret;
  3859. }
  3860. spin_lock(&dev_priv->mm.active_list_lock);
  3861. BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
  3862. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
  3863. spin_unlock(&dev_priv->mm.active_list_lock);
  3864. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3865. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3866. BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
  3867. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
  3868. mutex_unlock(&dev->struct_mutex);
  3869. ret = drm_irq_install(dev);
  3870. if (ret)
  3871. goto cleanup_ringbuffer;
  3872. return 0;
  3873. cleanup_ringbuffer:
  3874. mutex_lock(&dev->struct_mutex);
  3875. i915_gem_cleanup_ringbuffer(dev);
  3876. dev_priv->mm.suspended = 1;
  3877. mutex_unlock(&dev->struct_mutex);
  3878. return ret;
  3879. }
  3880. int
  3881. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3882. struct drm_file *file_priv)
  3883. {
  3884. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3885. return 0;
  3886. drm_irq_uninstall(dev);
  3887. return i915_gem_idle(dev);
  3888. }
  3889. void
  3890. i915_gem_lastclose(struct drm_device *dev)
  3891. {
  3892. int ret;
  3893. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3894. return;
  3895. ret = i915_gem_idle(dev);
  3896. if (ret)
  3897. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3898. }
  3899. void
  3900. i915_gem_load(struct drm_device *dev)
  3901. {
  3902. int i;
  3903. drm_i915_private_t *dev_priv = dev->dev_private;
  3904. spin_lock_init(&dev_priv->mm.active_list_lock);
  3905. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  3906. INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
  3907. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3908. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3909. INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
  3910. INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
  3911. INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
  3912. if (HAS_BSD(dev)) {
  3913. INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
  3914. INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
  3915. }
  3916. for (i = 0; i < 16; i++)
  3917. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3918. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3919. i915_gem_retire_work_handler);
  3920. spin_lock(&shrink_list_lock);
  3921. list_add(&dev_priv->mm.shrink_list, &shrink_list);
  3922. spin_unlock(&shrink_list_lock);
  3923. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3924. if (IS_GEN3(dev)) {
  3925. u32 tmp = I915_READ(MI_ARB_STATE);
  3926. if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
  3927. /* arb state is a masked write, so set bit + bit in mask */
  3928. tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
  3929. I915_WRITE(MI_ARB_STATE, tmp);
  3930. }
  3931. }
  3932. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3933. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3934. dev_priv->fence_reg_start = 3;
  3935. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3936. dev_priv->num_fence_regs = 16;
  3937. else
  3938. dev_priv->num_fence_regs = 8;
  3939. /* Initialize fence registers to zero */
  3940. if (IS_I965G(dev)) {
  3941. for (i = 0; i < 16; i++)
  3942. I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
  3943. } else {
  3944. for (i = 0; i < 8; i++)
  3945. I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
  3946. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3947. for (i = 0; i < 8; i++)
  3948. I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
  3949. }
  3950. i915_gem_detect_bit_6_swizzle(dev);
  3951. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3952. }
  3953. /*
  3954. * Create a physically contiguous memory object for this object
  3955. * e.g. for cursor + overlay regs
  3956. */
  3957. int i915_gem_init_phys_object(struct drm_device *dev,
  3958. int id, int size)
  3959. {
  3960. drm_i915_private_t *dev_priv = dev->dev_private;
  3961. struct drm_i915_gem_phys_object *phys_obj;
  3962. int ret;
  3963. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3964. return 0;
  3965. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3966. if (!phys_obj)
  3967. return -ENOMEM;
  3968. phys_obj->id = id;
  3969. phys_obj->handle = drm_pci_alloc(dev, size, 0);
  3970. if (!phys_obj->handle) {
  3971. ret = -ENOMEM;
  3972. goto kfree_obj;
  3973. }
  3974. #ifdef CONFIG_X86
  3975. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3976. #endif
  3977. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3978. return 0;
  3979. kfree_obj:
  3980. kfree(phys_obj);
  3981. return ret;
  3982. }
  3983. void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3984. {
  3985. drm_i915_private_t *dev_priv = dev->dev_private;
  3986. struct drm_i915_gem_phys_object *phys_obj;
  3987. if (!dev_priv->mm.phys_objs[id - 1])
  3988. return;
  3989. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3990. if (phys_obj->cur_obj) {
  3991. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3992. }
  3993. #ifdef CONFIG_X86
  3994. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3995. #endif
  3996. drm_pci_free(dev, phys_obj->handle);
  3997. kfree(phys_obj);
  3998. dev_priv->mm.phys_objs[id - 1] = NULL;
  3999. }
  4000. void i915_gem_free_all_phys_object(struct drm_device *dev)
  4001. {
  4002. int i;
  4003. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  4004. i915_gem_free_phys_object(dev, i);
  4005. }
  4006. void i915_gem_detach_phys_object(struct drm_device *dev,
  4007. struct drm_gem_object *obj)
  4008. {
  4009. struct drm_i915_gem_object *obj_priv;
  4010. int i;
  4011. int ret;
  4012. int page_count;
  4013. obj_priv = to_intel_bo(obj);
  4014. if (!obj_priv->phys_obj)
  4015. return;
  4016. ret = i915_gem_object_get_pages(obj, 0);
  4017. if (ret)
  4018. goto out;
  4019. page_count = obj->size / PAGE_SIZE;
  4020. for (i = 0; i < page_count; i++) {
  4021. char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4022. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4023. memcpy(dst, src, PAGE_SIZE);
  4024. kunmap_atomic(dst, KM_USER0);
  4025. }
  4026. drm_clflush_pages(obj_priv->pages, page_count);
  4027. drm_agp_chipset_flush(dev);
  4028. i915_gem_object_put_pages(obj);
  4029. out:
  4030. obj_priv->phys_obj->cur_obj = NULL;
  4031. obj_priv->phys_obj = NULL;
  4032. }
  4033. int
  4034. i915_gem_attach_phys_object(struct drm_device *dev,
  4035. struct drm_gem_object *obj, int id)
  4036. {
  4037. drm_i915_private_t *dev_priv = dev->dev_private;
  4038. struct drm_i915_gem_object *obj_priv;
  4039. int ret = 0;
  4040. int page_count;
  4041. int i;
  4042. if (id > I915_MAX_PHYS_OBJECT)
  4043. return -EINVAL;
  4044. obj_priv = to_intel_bo(obj);
  4045. if (obj_priv->phys_obj) {
  4046. if (obj_priv->phys_obj->id == id)
  4047. return 0;
  4048. i915_gem_detach_phys_object(dev, obj);
  4049. }
  4050. /* create a new object */
  4051. if (!dev_priv->mm.phys_objs[id - 1]) {
  4052. ret = i915_gem_init_phys_object(dev, id,
  4053. obj->size);
  4054. if (ret) {
  4055. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  4056. goto out;
  4057. }
  4058. }
  4059. /* bind to the object */
  4060. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  4061. obj_priv->phys_obj->cur_obj = obj;
  4062. ret = i915_gem_object_get_pages(obj, 0);
  4063. if (ret) {
  4064. DRM_ERROR("failed to get page list\n");
  4065. goto out;
  4066. }
  4067. page_count = obj->size / PAGE_SIZE;
  4068. for (i = 0; i < page_count; i++) {
  4069. char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4070. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4071. memcpy(dst, src, PAGE_SIZE);
  4072. kunmap_atomic(src, KM_USER0);
  4073. }
  4074. i915_gem_object_put_pages(obj);
  4075. return 0;
  4076. out:
  4077. return ret;
  4078. }
  4079. static int
  4080. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  4081. struct drm_i915_gem_pwrite *args,
  4082. struct drm_file *file_priv)
  4083. {
  4084. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4085. void *obj_addr;
  4086. int ret;
  4087. char __user *user_data;
  4088. user_data = (char __user *) (uintptr_t) args->data_ptr;
  4089. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  4090. DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
  4091. ret = copy_from_user(obj_addr, user_data, args->size);
  4092. if (ret)
  4093. return -EFAULT;
  4094. drm_agp_chipset_flush(dev);
  4095. return 0;
  4096. }
  4097. void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
  4098. {
  4099. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  4100. /* Clean up our request list when the client is going away, so that
  4101. * later retire_requests won't dereference our soon-to-be-gone
  4102. * file_priv.
  4103. */
  4104. mutex_lock(&dev->struct_mutex);
  4105. while (!list_empty(&i915_file_priv->mm.request_list))
  4106. list_del_init(i915_file_priv->mm.request_list.next);
  4107. mutex_unlock(&dev->struct_mutex);
  4108. }
  4109. static int
  4110. i915_gpu_is_active(struct drm_device *dev)
  4111. {
  4112. drm_i915_private_t *dev_priv = dev->dev_private;
  4113. int lists_empty;
  4114. spin_lock(&dev_priv->mm.active_list_lock);
  4115. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  4116. list_empty(&dev_priv->render_ring.active_list);
  4117. if (HAS_BSD(dev))
  4118. lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
  4119. spin_unlock(&dev_priv->mm.active_list_lock);
  4120. return !lists_empty;
  4121. }
  4122. static int
  4123. i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  4124. {
  4125. drm_i915_private_t *dev_priv, *next_dev;
  4126. struct drm_i915_gem_object *obj_priv, *next_obj;
  4127. int cnt = 0;
  4128. int would_deadlock = 1;
  4129. /* "fast-path" to count number of available objects */
  4130. if (nr_to_scan == 0) {
  4131. spin_lock(&shrink_list_lock);
  4132. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4133. struct drm_device *dev = dev_priv->dev;
  4134. if (mutex_trylock(&dev->struct_mutex)) {
  4135. list_for_each_entry(obj_priv,
  4136. &dev_priv->mm.inactive_list,
  4137. list)
  4138. cnt++;
  4139. mutex_unlock(&dev->struct_mutex);
  4140. }
  4141. }
  4142. spin_unlock(&shrink_list_lock);
  4143. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4144. }
  4145. spin_lock(&shrink_list_lock);
  4146. rescan:
  4147. /* first scan for clean buffers */
  4148. list_for_each_entry_safe(dev_priv, next_dev,
  4149. &shrink_list, mm.shrink_list) {
  4150. struct drm_device *dev = dev_priv->dev;
  4151. if (! mutex_trylock(&dev->struct_mutex))
  4152. continue;
  4153. spin_unlock(&shrink_list_lock);
  4154. i915_gem_retire_requests(dev);
  4155. list_for_each_entry_safe(obj_priv, next_obj,
  4156. &dev_priv->mm.inactive_list,
  4157. list) {
  4158. if (i915_gem_object_is_purgeable(obj_priv)) {
  4159. i915_gem_object_unbind(&obj_priv->base);
  4160. if (--nr_to_scan <= 0)
  4161. break;
  4162. }
  4163. }
  4164. spin_lock(&shrink_list_lock);
  4165. mutex_unlock(&dev->struct_mutex);
  4166. would_deadlock = 0;
  4167. if (nr_to_scan <= 0)
  4168. break;
  4169. }
  4170. /* second pass, evict/count anything still on the inactive list */
  4171. list_for_each_entry_safe(dev_priv, next_dev,
  4172. &shrink_list, mm.shrink_list) {
  4173. struct drm_device *dev = dev_priv->dev;
  4174. if (! mutex_trylock(&dev->struct_mutex))
  4175. continue;
  4176. spin_unlock(&shrink_list_lock);
  4177. list_for_each_entry_safe(obj_priv, next_obj,
  4178. &dev_priv->mm.inactive_list,
  4179. list) {
  4180. if (nr_to_scan > 0) {
  4181. i915_gem_object_unbind(&obj_priv->base);
  4182. nr_to_scan--;
  4183. } else
  4184. cnt++;
  4185. }
  4186. spin_lock(&shrink_list_lock);
  4187. mutex_unlock(&dev->struct_mutex);
  4188. would_deadlock = 0;
  4189. }
  4190. if (nr_to_scan) {
  4191. int active = 0;
  4192. /*
  4193. * We are desperate for pages, so as a last resort, wait
  4194. * for the GPU to finish and discard whatever we can.
  4195. * This has a dramatic impact to reduce the number of
  4196. * OOM-killer events whilst running the GPU aggressively.
  4197. */
  4198. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4199. struct drm_device *dev = dev_priv->dev;
  4200. if (!mutex_trylock(&dev->struct_mutex))
  4201. continue;
  4202. spin_unlock(&shrink_list_lock);
  4203. if (i915_gpu_is_active(dev)) {
  4204. i915_gpu_idle(dev);
  4205. active++;
  4206. }
  4207. spin_lock(&shrink_list_lock);
  4208. mutex_unlock(&dev->struct_mutex);
  4209. }
  4210. if (active)
  4211. goto rescan;
  4212. }
  4213. spin_unlock(&shrink_list_lock);
  4214. if (would_deadlock)
  4215. return -1;
  4216. else if (cnt > 0)
  4217. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4218. else
  4219. return 0;
  4220. }
  4221. static struct shrinker shrinker = {
  4222. .shrink = i915_gem_shrink,
  4223. .seeks = DEFAULT_SEEKS,
  4224. };
  4225. __init void
  4226. i915_gem_shrinker_init(void)
  4227. {
  4228. register_shrinker(&shrinker);
  4229. }
  4230. __exit void
  4231. i915_gem_shrinker_exit(void)
  4232. {
  4233. unregister_shrinker(&shrinker);
  4234. }